【问题标题】:Pascal exit procedure when other one called另一个调用时 Pascal 退出过程
【发布时间】:2012-07-15 14:09:13
【问题描述】:

所以我的问题看起来像,我有两个程序,它们互相调用,但这可能会导致溢出。如何跳转到程序 - 像 asm jmp 不调用?我不想使用标签,因为我不能在两个不同的过程之间使用 GoTo。而且我不希望执行调用过程之后的代码。顺便说一句,我使用 FPC。

unit sample;
interface

procedure procone;
procedure proctwo;

implementation

procedure procone;
begin
writeln('Something');
proctwo;
writeln('After proctwo'); //don't execute this!
end;

procedure proctwo;
begin
writeln('Something else');
procone;
writeln('After one still in two'); //don't execute this either
end;

end.

【问题讨论】:

  • 为什么你的程序包含你不想执行的代码?
  • 这非常复杂。正如我所说,我不想调用一个过程,我想跳转到一个不执行以下代码的过程。
  • A) 重写你的代码,这样就没有循环引用了。 B)删除您不想执行的代码。 C) 如果 A 或 B 不能解决您的问题,请发布您遇到的情况的真实示例并更好地描述问题,因为这个很愚蠢。

标签: overflow pascal procedures


【解决方案1】:

您必须使用函数参数来指示递归,以便函数知道它正在被相关函数调用。例如:

unit sample;

interface

procedure procone;
procedure proctwo;

implementation

procedure procone(const infunction : boolean);
begin
  writeln('Something');
  if infunction then exit;
  proctwo(true);
  writeln('After proctwo'); //don't execute this!
end;

procedure proctwo(const infunction : boolean);
begin
  writeln('Something else');
  if infunction then exit;
  procone(true);
  writeln('After one still in two'); //don't execute this either
end;

procedure usagesample;
begin
  writeln('usage sample');
  writeln;
  writeln('running procone');
  procone(false);
  writeln;
  writeln('running proctwo');
  proctwo(false);
end;

end.

usagesample 被调用时,它应该产生这个:

usage sample

running procone
Something
Something else
After proctwo

running proctwo
Something else
Something
After one still in two

【讨论】:

    猜你喜欢
    • 2017-05-23
    • 2011-07-18
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2016-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多