Pascal 中按值结果调用

发布于 2024-09-04 23:25:51 字数 331 浏览 4 评论 0原文

在这个例子中我如何模拟按值结果调用。无需添加变量且无需更改变量名称。?

Program one;
    var
      x:integer;
    Function two():integer;
        begin
           x:=x+1;
           two:=x;
        end;
    Procedure three(x:integer);
       begin
          x:=x+5;
          x:=two();
       end;
begin
x:=8;
three(x);
write(x);
end.

How can i simulate calling by value-result in this example. Without adding variables and without change a variable name.?

Program one;
    var
      x:integer;
    Function two():integer;
        begin
           x:=x+1;
           two:=x;
        end;
    Procedure three(x:integer);
       begin
          x:=x+5;
          x:=two();
       end;
begin
x:=8;
three(x);
write(x);
end.

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

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

发布评论

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

评论(3

自由如风 2024-09-11 23:25:51

如果这是家庭作业,我认为您不必费尽心思才能找到答案。

编辑:下面的评论是正确的 - 也许我引导你走上了错误的道路 - 最好回到我们原来的地方并从那里开始。

If it's homework, I don't think you will have to try var y hard to find the answer.

EDIT: The comment below is correct - maybe I was leading you up the wrong path - it's probably best to return to where we were and start from there.

寂寞陪衬 2024-09-11 23:25:51

对two() 做三() 已经做的事情。

Do to two() what three() already does.

云归处 2024-09-11 23:25:51

以目前的形式很难弄清楚这个问题的某些内容。

然而,如果我们假设 OP 询问如何在示例代码中模拟通过引用(而不是通过值)调用,那么这个问题似乎是有意义的。

所以...实际代码通过值将参数传递给two()two() 似乎试图在调用 two() 之前将值存储到全局变量,而这又会更改全局变量。

但全局变量实际上是用来传递值的,所以技巧是,在将全局按值传递给三()后,全局最终会发生变化,就像通过引用传递一样。

只是有一个小问题(这似乎构成了整个问题):由于参数的名称与全局的名称相同,如何从 two() 内部解决全局问题?

我不知道应该运行此代码的 Pascal 版本,但如果编译器与 TP/Delphi 系列兼容并且 var 名称必须保持不变,那么我会尝试迄今为止我所知道的唯一方法:one.x。具体来说,two() 实现中的这一行

x:=x+5;

我将更改为

one.x:=x+5;

因此,这里的全局接收参数值增加 5 并调用 two(),并且 two() 再次改变全局。

It's hard to make out something of the question in its present form.

However, the question seems to make some sense if we suppose that the OP is asking how to emulate calling by reference (not by value) in the example code.

So... The actual code passes the parameter to three() by value. three() seems to be trying to store the value to the global variable, before calling two() which, in its turn, changes the global.

But the global variable is actually being used to pass the value, and so the trick is that after passing the global by value to three() the global will end up changed, as if it were passed by reference.

Only there's a little question (which seems to constitute the whole problem): how to address the global from within three() since the parameter's name is the same as the global's?

I don't know what version of Pascal is supposed to run this code, but if the compiler is compatible with TP/Delphi family and if the var names must be left unchanged, then I would try the only way I am aware of so far: one.x. Specifically, this line in three()'s implementation

x:=x+5;

I would change to

one.x:=x+5;

So, the global here receives the parameter value increased by 5 and calls two(), and two() changes the global once again.

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