Is it possible in modern Fortran to return an array from a function with a performance equivalent to the fact that the routine will fill the array passed as an argument?
Consider, for example, as a simple example
PROGRAM PRETURN
INTEGER :: C(5)
C = FUNC()
WRITE(*,*) C
CALL SUB(C)
WRITE(*,*) C
CONTAINS
FUNCTION FUNC() RESULT(X)
INTEGER :: X(5)
X = [1,2,3,4,5]
END FUNCTION FUNC
SUBROUTINE SUB(X)
INTEGER :: X(5)
X = [1,2,3,4,5]
END SUBROUTINE SUB
END PROGRAM PRETURN
Here, the string C = FUNC()will copy the values from the return value of the function before dropping the returned array from the stack. The subroutine version CALL SUB(C)will populate directly C, avoiding the extra step of overcoming and using memory associated with a temporary array - but make it impossible to use in expressions such as SUM(FUNC()).
, , , C, . *
, - ?
* , . Intel fortran (re) , , ALLOCATE(C, SOURCE=FUNC()). Gfortran , , ALLOCATE, SOURCE, .