- Thread starter
- #21
NickFort
Technical User
- Jun 10, 2010
- 113
Thanks, mikrom, I appreciate it. Glad I could help!
While I'm here, an explanation from Tobias regarding why the subroutine works, but the function doesn't:
--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
While I'm here, an explanation from Tobias regarding why the subroutine works, but the function doesn't:
in the main program:
real, dimension), allocatable :: vector_call
call random_vector(vector_call)
Here, you pass (effectively) the address of the array descriptor to the
subroutine "random_vector" - and that variable gets evaluated.
real, dimension), allocatable :: vector_call
vector_call = random_vector()
Whereas in the function case, you allocate the result variable "vector"
- but not the variable "vector_call". Fortran works such that first all
on the RHS of an assignment is evaluated and then the result assigned to
the LHS. Thus, the RHS is allocated, but gfortran assumes (as does
Fortran 90/95) that the LHS is allocated with the correct size - and
does not touch the allocation status or the size (rather: shape) of the LHS.
--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.