Fortran 中结果变量的用途是什么? [英] What is the purpose of result variables in Fortran?

查看:17
本文介绍了Fortran 中结果变量的用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Fortran 中,有两种标准方法可以从函数返回结果.第一种是将函数的返回值赋给函数名.

In Fortran, there are two standard ways to return a result from a function. The first one is by assigning the return value of the function to the function name.

function foo()
    integer :: foo

    foo = 10
end function foo

在 Fortran 90 中标准化的第二种形式是通过结果"变量.

The second form, standardized in Fortran 90 is through a "result" variable.

function foo result(res)
    integer :: res

    res = 10
 end function foo

调用任一形式的函数都会返回值 10.我的问题是,Fortran 90 委员会引入结果变量的基本原理是什么?他们是否标准化了一种常见的做法?或者他们是否允许程序通过不将函数名称与函数结果绑定来更加模块化.例如,在 foo() 的第二个版本中,函数 foo() 的名称可以更改为 bar() 并且函数在调用时仍会按预期工作.

Calling either form of the function returns the value 10. My question is, what was the rationale of the Fortran 90 committee for introducing result variables? Were they standardizing a common practice? Or were they allowing programs to be more modular by not tying the function name to a function result. For example, in the second version of foo(), the name of the function foo() could be changed to bar() and the function would still work as expected when called.

但是,我可能错了.有谁知道引入结果变量的实际原理是什么?

However, I may be wrong. Does anyone know what the actual rationale was for introducing result variables?

推荐答案

resultrecursion的同时引入.在我们了解递归函数是如何产生的之前,先澄清一下结果变量是什么.

Introduced at the same time as the result was recursion. Before we get to how a recursive function comes about, some clarification on what a result variable is.

无论是否使用result,函数的结果总是通过一个result变量返回.1result的result变量有指定名称,如果没有它,结果变量与函数具有相同的名称.在后一种情况下,名称的使用是对结果变量而不是函数的引用.

The function result is always returned through a result variable, whether result is used or not.1 With result the result variable has the name specified, and without it the result variable has the same name as the function. In this latter case use of the name is a reference to the result variable and not the function.

所以,如果函数 foo 有结果变量 foo 那么我们不能直接递归:

So, if the function foo has result variable foo then we can't do direct recursion:

recursive function foo(n)
  foo = foo(n-1) ! Oh dear
end function

result的出现是为了让我们可以拥有

result comes about so that we can have

recursive function foo(n) result(res)
  res = foo(n-1) ! Yay
end function

<小时>

[1] 嗯,直到 Fortran 2008 年,variable 的定义发生了变化,情况都是如此.对于现代 Fortran,请使用术语 函数结果.


[1] Well, this is true up until Fortran 2008, when the definition of variable changed. For modern Fortran use instead the term function result.

这篇关于Fortran 中结果变量的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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