Function in fortran, passing array in, receiving array out

With RESULT(FluxArray), fluxArray is the name of the result variable. As such, your attempt to declare the characteristics in the result clause are mis-placed.

Instead, the result variable should be specified within the function body:

function Flux(W1,W2) result(fluxArray)
  double precision, dimension(3), intent(in)::W1, W2
  double precision, dimension(3) :: fluxArray  ! Note, no intent for result.
end function Flux

Yes, one can declare the type of the result variable in the function statement, but the array-ness cannot be declared there. I wouldn’t recommend having a distinct dimension statement in the function body for the result variable.

Note that, when referencing a function returning an array it is required that there be an explicit interface available to the caller. One way is to place the function in a module which is used. See elsewhere on SO, or language tutorials, for more details.

Coming to the errors from your question without the result.

DOUBLE PRECISION FUNCTION Flux(W1,W2)
  DOUBLE PRECISION, DIMENSION(3), INTENT(OUT):: Flux

Here the type of Flux has been declared twice. Also, it isn’t a dummy argument to the function, so as above it need not have the intent attribute.

One could write

FUNCTION Flux(W1,W2)
  DOUBLE PRECISION, DIMENSION(3) :: Flux  ! Deleting intent

or (shudder)

DOUBLE PRECISION FUNCTION Flux(W1,W2)
  DIMENSION :: Flux(3)

The complaint about a statement function is unimportant here, following on from the bad declaration.

Leave a Comment