如何理解Fortran中没有参数和括号的子程序。

huangapple go评论59阅读模式
英文:

How to understand a subroutine without argument and bracket in Fortran

问题

学习F90时,我看到大多数教程甚至一些书籍教导子程序的格式如下:

subroutine process_name (opt_args)
!主要代码在此处
end subroutine process_name


然而,我目前正在审查一个用F90编写的模型,它在单独的文件中定义了许多子程序,格式如下:

subroutine process_name
!主要代码在此处
end


我猜这两种方式都是正确的,但我对这两种结构背后的原因(或历史)很感兴趣。如果有人能提供一些解释,我将不胜感激。关于Fortran 90编程的任何书籍建议也将非常棒。

我尝试了两种方法,它们都有效。
英文:

when learning F90, I saw most of tutorials or even some books teach that a subroutine has a format as below:

subroutine process_name (opt_args) 
  !main_codes_go_here
end subroutine process_name

Yet, I am currently reviewing a model written in F90, which defines many subroutines in separate files that have a format as below:

subroutine process_name 
  !main_codes_go_here
end 

I guess both ways are correct, but I am curious about the reasons (or history) behind the two structures. I would greatly appreciate if anyone can give some explainations? Suggestions about any book on Fortran 90 programming will be awesome as well.

I tried both approach, and they all work.

答案1

得分: 1

这只是一个结构,如果需要的话,可以用一个单独的EBNF节点来描述。

简单的解释是,如果参数为空,括号是可选的,只需使用end而不是end subroutine,如果使用完整的end subroutine,还可以在其后添加subroutine的名称。

然而,在旧的Fortran 90中并不是那么简单。即使在Fortran 95和2003中也一样。

在Fortran 77及更早版本中,唯一的选项是end。在Fortran 90中,您可以添加单词subroutine,如果这样做,还可以添加名称。但在某些地方,您必须使用完整的end subroutine。特别是在模块和内部过程中。

在Fortran 2008中,解除了这个限制,您也可以在模块和内部过程中使用end

对于函数来说也类似。只有在Fortran 77中可以使用end。在Fortran 90-2003中,对于模块和内部函数,需要使用end function。在Fortran 2008中,您也可以仅使用end

英文:

This is relly just one structure, describable, if needed, by a single EBNF node.

The simple explanation is that if the arguments are empty, the parentheses are optional, that just end can be used instead of end subroutine and that if you use the full end subroutine you can also add the name of the subroutine after that.

However, it is not that simple in the old Fortran 90. And even in Fortran 95 and 2003.

In Fortran 77 and earlier, the only option was just end. In Fortran 90 you could add the word subroutine and if you did you could also add the name. But in some locations you had to use the full end subroutine. Namely in module and internal procedures.

In Fortran 2008 this restriction was removed and you can use just end also for module and internal procedures.

For functions it is similar. Only end up to Fortran 77. In Fortran 90-2003 end function is required for module and internal functions. In Fortran 2008 you can use just end also for these.

huangapple
  • 本文由 发表于 2023年5月14日 12:41:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76245834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定