Whether to use one or another is more or less a matter of programming style. You are allowed to write the arguments of both functions and subroutines as intent(in)
, intent(inout)
or intent(out)
.
My personal style is however to only use the intent(in)
arguments for functions, which is also a requirement for pure
functions. An exception to this rule can be made when en error code intent(out)
argument is necessary.
There is a subtle trap hidden in functions which return different results for the same input argument value. Consider a hypothetical function returning a random number
real function rnd()
end function
calling it once
x = rnd()
is completely OK. Calling it multiple times in a single expression
x = (rnd() + rnd()) / 2
can result in the function being called only once. Fortran language rules allow such behaviour. Therefore, the standard Fortran procedure for getting random numbers random_number()
is a subroutine (and because all intrinsic functions are pure
).
Where ever you cannot use a function, use a subroutine.
Any function can by converted to a subroutine by moving the result variable to a dummy argument with intent(out)
. The opposite process may be more problematic.
CALL
statement and thus cannot be part of an expression. – Cohette