返回多个值的函数

Har*_*ura 7 fortran gfortran fortran90

在Fortran中,是否可以定义一个返回多个值的函数,如下所示?

[a, b] = myfunc(x, y)
Run Code Online (Sandbox Code Playgroud)

Ale*_*ogt 8

这取决于......因此functions,不可能有两个不同的功能结果.但是,您可以从函数返回一个长度为2的数组.

  function myfunc(x, y)
    implicit none
    integer, intent(in) :: x,y
    integer             :: myfunc(2)

    myfunc = [ 2*x, 3*y ]
  end function
Run Code Online (Sandbox Code Playgroud)

如果需要两个返回值到两个不同的变量,请使用subroutine:

  subroutine myfunc(x, y, a, b)
    implicit none
    integer, intent(in) :: x,y
    integer, intent(out):: a,b

    a = 2*x
    b = 3*y
  end subroutine
Run Code Online (Sandbox Code Playgroud)