如何从函数调用中全局化虚拟参数

发布于 2024-07-28 23:31:55 字数 378 浏览 4 评论 0原文

如何从函数调用中全局化虚拟参数?

我在 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.

,问题是 ab 的值没有为第二次调用保留。 它返回的是垃圾。 即使我尝试使用 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 技术交流群。

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

发布评论

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

评论(3

蓝眸 2024-08-04 23:31:55

你说“ab是从函数调用填充的值,但它们没有在任何地方声明” - 尽管我仍在试图弄清楚你到底是什么当我试图这样做时,这个声明引发了各种危险信号。 这是 Fortran - 您需要声明它们(参见注释)。

我不清楚你想要实现什么 - 不过,“全球化”可能不是你想要做的 - 我们应该努力消除代码中的全局变量。

注意:是的,Fortran(或者实际上是 FORTRAN)支持变量的隐式声明。 然而,这是打孔卡和哑终端时代的遗物。 不要在任何地方使用隐式类型 - 您的代码应该始终包含隐式类型,并且您应该适当地声明变量。

You say that "a and b 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.

Saygoodbye 2024-08-04 23:31:55

使用 SAVE 声明来存储子例程或函数调用之间的状态。

Use the SAVE declaration to store the state across invocations of the subroutine or function.

不念旧人 2024-08-04 23:31:55

虚拟变量本质上是:虚拟参数。 它们只是占位符,当调用函数时,它们会被替换为调用函数时传递给函数的任何内容。 让我们看一个愚蠢的例子:

integer function add(x,y)
  integer :: x, y
  add = x + y
end

现在,您可以通过多种方式调用这个函数。 假设您要添加 a 和 b:

integer :: a, b, c

a = 3
b = 4
c = add(a,b)

这里,a 将占据函数中 x 的位置,b 将占据 y 的位置。 c 的值为 7。您可以轻松完成:

integer :: c

c = add(3,4)

得到相同的结果。 正如你所看到的,全球化虚拟参数是没有意义的。

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:

integer function add(x,y)
  integer :: x, y
  add = x + y
end

so now, you can call this function in many ways. lets say you want to add a and b:

integer :: a, b, c

a = 3
b = 4
c = add(a,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:

integer :: c

c = add(3,4)

with the same results. as you can see, globalizing dummy arguments is meaningless.

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