fortran - 返回多个值的函数

标签 fortran fortran90 gfortran

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

[a, b] = myfunc(x, y)

最佳答案

这取决于...使用函数,不可能有两个不同的函数结果。但是,您可以从该函数返回一个长度为 2 的数组。

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

    myfunc = [ 2*x, 3*y ]
  end function

如果您需要两个不同变量的两个返回值,请使用子例程:

  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

关于fortran - 返回多个值的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37120263/

相关文章:

compiler-errors - 错误: Too Few Subscripts for Specified for Array

module - Fortran 中模块使用的模块的变量范围

linux - 添加标准路径以查找 .o 文件 gfortran

fortran - Fortran 95 编译器可以编译 Fortran 77 代码吗?

fortran - gfortran 不允许具有不同组件长度的字符数组

javascript - 将 Fortran 语言转换为 Javascript

c++ - 使用带 nvcc 和 CUSP 的 IFORT 的未解析引用

arrays - 从文件读取并存储在数组中的问题

windows - 使用cygwin在Linux和Windows中编译的MPI fortran代码

c - Fortran 2003 绑定(bind)到 C : how to translate enums and #defines? 中的库