For optimisation reasons, Fortran enforces that the dummy arguments of a subroutine or function are not alias, i.e., they do not point the the same memory place.
I am wondering if the same constraint applies to the returned value of a function. In other words, for a given myfunc function:
function myfunc(a)
real, intent(in) :: a(:)
real :: myfunc(size(a))
myfunc = a * 2
end function myfunc
is it standard-compliant to write: a = myfunc(a) and b = myfunc(a) ?
The arguments of a function and function return value are different things. Contrary the previous answer, the functional arguments ARE passed by reference, or by copy-in copy-out, unless they are declared as dummy arguments with the
VALUE
attribute. This is a major difference of Fortran vs. C.However, if the function value is constructed by normal assignment (=) and not by pointer assignment (=>) they are separate entities. In your code, the value of myfunc is got by copying the value of a. Therefore no standard rules are broken by
a = myfunc(a)
orb = myfunc(a)
.Since the variable a in myfunc is a separate entity from a that is being passes as a dummy argument from the parent routine, it is perfectly fine to do:
or
There is no conflict here because value of a is being copied to the dummy argument inside the function, the function is being evaluated, and in the end the value of the function is being copied to a.
From Fortran 2008 Standard draft:
In general, it is good practice to force functions to not have side-effects, e.g. use
PURE
attribute and declareINTENT
for all dummy arguments.