如何在Fortran中声明返回数组的函数类型?

如何在Fortran中声明返回数组的函数类型?,fortran,Fortran,我有一个返回数组的函数,比如 function f(A) implicit none real, intent(in) :: A(5) real, intent(out) :: f(5) f = A+1 end 我的问题是,如何在主程序单元中定义f?例如 program main implicit none real :: A(5) real, dimension(5), external :: f ! does not work

我有一个返回数组的函数,比如

function f(A)
    implicit none
    real, intent(in) :: A(5)
    real, intent(out) :: f(5)

    f = A+1
end
我的问题是,如何在主程序单元中定义
f
?例如

program main
    implicit none
    real :: A(5)
    real, dimension(5), external :: f  ! does not work

    ...
end 

您需要一个显式接口。你可以用几种方法来做到这一点

  • 显式地在调用
    f
    的作用域单元中:

    interface
      function f(A)
        implicit none
        real, intent(in) :: A(5)
        real :: f(5)
      end function
    end interface
    
  • 将该函数作为内部函数放在程序主机作用域中:

     program main
        ...
     contains
       function f(A)
         implicit none
         real, intent(in) :: A(5)
         real :: f(5)
    
         f = A+1
       end
     end program
    
  • 将功能放在模块中:

     module A
     contains
       function f(A)
         implicit none
         real, intent(in) :: A(5)
         real :: f(5)
    
         f = A+1
       end
     end module
    
     program main
       use A
       ...
     end program
    
  • 使用具有相同参数和返回类型、种类和等级的不同过程的显式接口

    program main
      interface
        function r5i_r5o(r5)
          implicit none
          real, intent(in) :: r5(5)
          real :: r5i_r5o(5)
        end function
      end interface
    
      procedure(r5i_r5o) :: f
      ...
    end program
    
    function f(A)
      implicit none
      real, intent(in) :: A(5)
      real :: f(5)
    
      f = A+1
    end
    

  • 最干净的方法是使用模块的选项#3。这为您提供了自动显式界面的好处(无需在调用
    f
    的任何地方执行选项#1),并使您的功能在使用模块的任何地方都可用,而不是像选项#2那样局限于特定的范围单元。如果您有许多具有相同参数和返回类型的过程,则选项#4非常方便,因为一个显式接口可用于所有过程。

    这显示了指定函数结果的三种不同方法,以及如何使用模块组织函数:

    module so_func
    
        INTEGER, PARAMETER :: MAX_SIZE = 5
    
        TYPE MY_DATA
            INTEGER :: SIZE
            REAL, DIMENSION(MAX_SIZE) :: DATA
        ENDTYPE
    
    
    contains
    
        FUNCTION f1(A,N) RESULT(X)
        implicit none
        INTEGER, INTENT(IN) :: N
        REAL, INTENT(IN) :: A(N)
        REAL :: X(N)
        ! ....
        X = 1.0+A
        END FUNCTION f1
    
        TYPE(MY_DATA) FUNCTION f2(A,N)
        implicit none
        INTEGER, INTENT(IN) :: N
        REAL, INTENT(IN) :: A(N)
        ! ....
        f2%SIZE = N
        f2%DATA(1:N) = 1.0+A
        END FUNCTION f2
    
        FUNCTION f3(A,N)
        implicit none
        INTEGER, INTENT(IN) :: N
        REAL, INTENT(IN) :: A(N)
        REAL :: f3(N)
        ! ....
        f3 = 1.0+A
        END FUNCTION f3
    
    end module
    
    
    program SO_RESULT
        use so_func
        implicit none
        integer, parameter :: n=5
        REAL :: A(n), y1(n), y3(n)    
        TYPE(MY_DATA) :: y2
        INTEGER :: i
        ! Variables
    
        A =(/ (i, i=1,n) /)
    
        y1 = f1(A,n)
        y2 = f2(A,n)
        y3 = f3(A,n)
    
    
    end program SO_RESULT
    

    对于返回数组的函数,需要显式接口。例如,请参见。