Fortran 返回语句

发布于 2024-09-08 19:38:06 字数 405 浏览 4 评论 0原文

我试图让一些在 gfortran 下编译的代码在 g77 下编译良好。问题似乎来自 return 语句:

ffuncs.f:934.13:

  RETURN E
         1

Error: Alternate RETURN statements at (1) require a SCALAR-INTEGER return specifier

在代码中,任何 E 都被指定为实数*8:

IMPLICIT REAL*8 ( A - H , O -Z )

但是,E 从未被赋予值或任何事实上你在 return 语句之前从未看到它的东西。我对fortran几乎一无所知。 Fortran 中带参数的 return 语句的含义是什么?

谢谢。

I'm trying to get some code compiled under gfortran that compiles fine under g77. The problem seems to be from a return statement:

ffuncs.f:934.13:

  RETURN E
         1

Error: Alternate RETURN statement at (1) requires a SCALAR-INTEGER return specifier

In the code anything E was specified as real*8:

IMPLICIT REAL*8 ( A - H , O -Z )

However, E was never given a value or anything in fact you never see it until the return statement. I know almost nothing about fortran. What is the meaning of a return statement with an argument in fortran?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白云悠悠 2024-09-15 19:38:06

在 FORTRAN 中(直到我非常熟悉的 Fortran 77),RETURN n 不用于返回函数值;相反,它执行的操作类似于其他语言中异常处理的操作:退出到正常位置以外的代码位置。

您通常会使用标签作为参数来调用这样的 SUBROUTINEFUNCTION ,例如,

  CALL MYSUB(A, B, C, *998, *999)
...
998 STOP 'Error 1'
998 STOP 'Error 2'

如果 MYSUB 中出现问题,那么您会执行 RETURN 1RETURN 2(而不是正常的 RETURN),您会在调用例程中直接跳到标签 998 或 999。

这就是为什么通常您希望 RETURN 上有一个整数 - 它不是一个值,而是您想要采取的错误退出的索引。

RETURN E 对我来说听起来不对。除非有我不知道的语法,否则以前的编译器应该将其标记为错误。

In FORTRAN (up to Fortran 77, which I'm very familiar with), RETURN n is not used to return a function value; instead, it does something like what in other languages would be handled by an exception: An exit to a code location other than the normal one.

You'd normally call such a SUBROUTINE or FUNCTION with labels as arguments, e.g.

  CALL MYSUB(A, B, C, *998, *999)
...
998 STOP 'Error 1'
998 STOP 'Error 2'

and if things go wrong in MYSUB then you do RETURN 1 or RETURN 2 (rather than the normal RETURN) and you'd be hopping straight to label 998 or 999 in the calling routine.

That's why normally you want an integer on that RETURN - it's not a value but an index to which error exit you want to take.

RETURN E sounds wrong to me. Unless there's a syntax I'm unaware of, the previous compiler should have flagged that as an error.

逐鹿 2024-09-15 19:38:06

在 Fortran 函数中,通过将值分配给与函数同名的假变量来返回值。一旦你这样做了,只需返回即可。

In a Fortran function one returns the value, by assigning the value to a fake variable which is the same name as the function. Once you do that, simply return.

吃颗糖壮壮胆 2024-09-15 19:38:06

我认为@Carl Smotricz 已经给出了答案。 ffuncs 的参数列表是否具有星号虚拟参数(以匹配调用中的星号标签)?或者是在没有替代回报的情况下使用了此方法?如果没有替代回报,只需删除“E”即可。如果有替代返回,那么最大的问题是程序在运行时之前做了什么,因为变量的类型错误且未初始化。如果变量没有与预期分支之一匹配的整数值,则程序可能采用常规返回分支 - 但这只是一种猜测 - 如果是这样,简单的解决方法是再次删除“E”。

“交替返回”功能被语言标准视为“过时”,可能会在未来的标准中删除;如果它因遗留代码而被删除,编译器可能会继续支持它。对于新代码,一种简单的替代方法是返回整数状态变量并在调用方中使用“select case”语句。

I think @Carl Smotricz has the answer. Does argument list of ffuncs has dummy arguments that are asterisks (to match the asterisk-label in the calls)? Or was this used without there being alternative returns? If there were no alternative returns, just delete the "E". If there are alternative returns, the big question is what the program was doing before at run time since the variable was of the wrong type and uninitialized. If the variable didn't have an integer value matching one of the expected branches, perhaps the program took the regular return branch -- but that's just a guess -- if so, the easy fix is to again to delete the "E".

The "alternate return" feature is considered "obsolescent" by the language standard and could be deleted in a future standard; compilers would likely continue to support it if it were removed because of legacy code. For new code, one simple alternative is to return an integer status variable and use a "select case" statement in the caller.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文