返回多个值的函数 [英] Function which returns multiple values

查看:214
本文介绍了返回多个值的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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


推荐答案

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

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

这篇关于返回多个值的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆