如何从函数调用中全局化虚拟参数
如何从函数调用中全局化虚拟参数?
我在 Fortran 中有下面的代码
A(a,b) // here a and b are the values filling from function call but they are not declared any where
.
.
.
B(a.b) // same variable passing again to this function call also.
,问题是 a 和 b 的值没有为第二次调用保留。 它返回的是垃圾。 即使我尝试使用 common 但它不接受全球化虚拟参数。 怎么做?
How to globalize a dummy argument from a function call?
I have the below code in Fortran
A(a,b) // here a and b are the values filling from function call but they are not declared any where
.
.
.
B(a.b) // same variable passing again to this function call also.
here the problem is the values from a and b are not maintaining for second call. it's returning a garbage. Even I tried this using common but its not accepting to globalize dummy arguments. How to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你说“
a
和b
是从函数调用填充的值,但它们没有在任何地方声明” - 尽管我仍在试图弄清楚你到底是什么当我试图这样做时,这个声明引发了各种危险信号。 这是 Fortran - 您需要声明它们(参见注释)。我不清楚你想要实现什么 - 不过,“全球化”可能不是你想要做的 - 我们应该努力消除代码中的全局变量。
注意:是的,Fortran(或者实际上是 FORTRAN)支持变量的隐式声明。 然而,这是打孔卡和哑终端时代的遗物。 不要在任何地方使用隐式类型 - 您的代码应该始终包含
隐式类型
,并且您应该适当地声明变量。You say that "
a
andb
are the values filling from function call but they are not declared anywhere" - even though I'm still trying to figure out exactly what you're trying to do, this statement throws up all sorts of red flags. This is Fortran - you need to declare them (see note).I'm not clear on what you're trying to accomplish - chances are, though, "globalize" isn't what you want to do - we should be striving to eliminate global variables in our code.
Note: Yes, Fortran (or really, FORTRAN) supports implicit declaration of variables. However, this is a relic from the days of punch cards and dumb terminals. Do Not use implicit typing anywhere - your code should always include
implicit none
, and you should declare your variables appropriately.使用
SAVE
声明来存储子例程或函数调用之间的状态。Use the
SAVE
declaration to store the state across invocations of the subroutine or function.虚拟变量本质上是:虚拟参数。 它们只是占位符,当调用函数时,它们会被替换为调用函数时传递给函数的任何内容。 让我们看一个愚蠢的例子:
现在,您可以通过多种方式调用这个函数。 假设您要添加 a 和 b:
这里,a 将占据函数中 x 的位置,b 将占据 y 的位置。 c 的值为 7。您可以轻松完成:
得到相同的结果。 正如你所看到的,全球化虚拟参数是没有意义的。
Dummy variables are by nature, that: dummy arguments. They are simply placeholders, and when a function is called, they get replaced with whatever you passed the function when calling it. lets look at a silly example:
so now, you can call this function in many ways. lets say you want to add a and b:
here, a will take the position of x in the function, and b will take the position of y. c will have the value of 7. you could have just as easily done:
with the same results. as you can see, globalizing dummy arguments is meaningless.