Function in fortran, passing array in, receiving array out

。_饼干妹妹 提交于 2019-11-27 05:30:00

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!