Allocatable array as function dummy argument

Can I use an allocatable array as a function dummy argument? If yes, is there any situation this is needed?

There is no restriction on that. Arguments to a function are not required to be intent(in), as you might think. It is merely a choice. I have seen libraries that use the return value of the function to pass on status (error) information and happily change any or all the arguments that are passed. Whether that is good practice, is another matter. I can imagine good arguments against doing so, but also arguments for it.

More generally, allocatable arrays may be used anywhere a non-allocatable array may be used. That they are allocatable matters in a procedure call only if you wish to change the allocation status in the procedure, in which case you must declare the dummy (procedure) argument to be ALLOCATABLE.


I would add that the allocatable specification allows range information to be transferred to the function, while with non-allocatable array input one has to guess it:

! Array bounds from the array
integer, allocatable, intent(in) ::x(:)

! Array bounds redefined for this routine only
integer, intent(in) :: y(-32:) 

That’s also true for pointer, and with intent(in) it isn’t required that the actual argument be a pointer, unlike the allocatable case where the actual argument must be allocatable.

I’d disagree that “one has to guess it”. What you’re talking about is that, in the non-pointer/allocatable case, the lower bounds of an assumed-shape array are always 1 and the upper bounds are the extents (number of elements). This makes writing procedures much simpler, especially given that the actual argument can be an array section or vector-subscripted.


Right - this flexible array handling is perhaps one of the most useful features of Fortran IMHO - despite most non-fortran chatter apparently believing “all loops in fortran start at 1”, while “they should start at 0”. Arrays starting at 0 are certainly a legit choice, which Fortran’s always allowed, even though they’re highly counter-intuitive to me (0 means “nothing” after all).


Note the standard here stipulates “the actual argument shall be a pointer or a valid target for the dummy pointer in a pointer assignment statement.” Under ordinary circumstances, the latter leads to the actual argument to have the TARGET attribute which isn’t always enforceable and thus there are limits to its utility. Also, this extension was introduced to the standard starting Fortran 2008 and the processors vary in their quality of implementation.

As you will know, FUNCTIONs in Fortran that are not qualified additionally can possibly have side effects. The onus then lies on the program author and the consumer(s) to deal with the consequences.

FUNCTIONs attributed additionally e.g., PURE / ELEMENTAL , SIMPLE (feature introduced starting Fortran 2023) have particular semantic requirements designed to avoid certain side effects. These start with the INTENT(IN) attribute of function parameters. With this attribute, ALLOCATABLE array dummy arguments are not a concern.