Function in fortran, passing array in, receiving array out

后端 未结 1 1439
孤独总比滥情好
孤独总比滥情好 2020-11-30 14:06

I have this function, depicted below. It passes in two vectors with three values each, and should pass out one vector with three values as well. I call the function like thi

相关标签:
1条回答
  • 2020-11-30 14:46

    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.

    0 讨论(0)
提交回复
热议问题