修正fortran函数语法 [英] Correct fortran function syntax

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

问题描述

 函数func(i)result(j)
...
结束函数fun

这里是第二个例子

 实函数func(x)
...
结束函数func

这是第三个例子

 实函数func(x )
...
返回
结束函数func

其中按照现代fortran标准,一种是合适的语法?在第二和第三个例子中,如果我在real类型的程序中声明了多个变量,编译器如何知道哪个变量返回?

解决方案

根据具体情况,所有示例都是合适的。如果打开的函数语句没有RESULT说明符,那么函数体内的函数结果将与名称相同该功能。



您可以在打开的函数语句中使用RESULT说明符,如第一个示例中所示,将函数结果的名称更改为其他名称。

您可能希望这样做,因为您更喜欢为函数名称以外的函数结果使用名称。你可能需要这样做,如果你真的想要递归地调用函数。

你可以在开始语句中指定函数的类型(按照你的第二个第三个例子),或者你可以在函数本身的规范部分(在主体中)指定函数的类型(这是大概在你的第一个例子中发生的情况,隐式键入)。函数结果的属性必须在主体中指定,因此作为风格问题,有些人更愿意将所有内容都放在函数体中(这也避免了与范围和定义顺序相关的一些模糊复杂问题)。你不应该使用隐式类型。



最后一个例子在函数的可执行部分末尾只有一个显式的return语句。这是多余的 - 当执行到达函数的可执行部分的末尾时,该函数自动返回。有些人更喜欢明确的 return ,也许是因为他们深深地担心编译器可能会突然忘记自动返回,其他人则认为这是荒谬的愚蠢行为。



重复 - 如果在打开的函数语句中没有RESULT子句,则函数结果的名称是函数名称,否则它是结果子句中的名称。


I am confused between various syntax for functions in fortran.

 function func(i) result(j)
...
end function fun

Here is second example

 real function func (x)
...
end function func

This is third example

  real function func (x)
    ...
    return
    end function func

Which one is appropriate syntax as per modern fortran standard? And in the 2nd and 3rd example, if I declare multiple variables in the program of type real, how does compiler know which variable to return?

解决方案

All of your examples are appropriate, depending on circumstances. Aspects of this come down to style preferences.

If the opening function statement does not have a RESULT specifier, inside the body of the function the function result has the same name as the function.

You can use the RESULT specifier in the opening function statement, as in your first example, to change the name of the function result to something else.

You might want to do this because you prefer to use a name for the function result other than the function name. You might need to do this if you actually want to recursively invoke the function.

You can either specify the type of the function in the opening statement (as per your second or third examples), or you can specify the type of the function in the specification part (in the body) of the function itself (which is what is presumably happening in your first example, implicit typing aside). Attributes of the function result must be specified in the body, so as a matter of style some people prefer to keep everything together in the function body (this also avoids some obscure complications associated with scope and the ordering of definitions). You should not be using implicit typing, ever.

Your last example simply has an explicit return statement at the end of the executable part of the function. This is superfluous - the function automatically returns when execution reaches the end of the executable part of the function. Some people prefer the explicit return, perhaps because they have a deep seated fear that the compiler might suddenly forget to automatically return, others think that this is absurd foolishness.

Repeating - the name of the function result is the function name, if there is no RESULT clause in the opening function statement, otherwise it is the name in the result clause.

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

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