Function which returns multiple values

旧街凉风 提交于 2019-12-08 19:55:37

问题


In Fortran, is it possible to define a function which returns multiple values like below?

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

回答1:


That depends... With functions, it is not possible to have two distinct function results. However, you can have an array of length two returned from the function.

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

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

If you need two return values to two distinct variables, use a subroutine instead:

  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


来源:https://stackoverflow.com/questions/37120263/function-which-returns-multiple-values

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