首页 > 解决方案 > 如何从内部(重新)调用函数,而不将原始调用返回给发件人

问题描述

这是我试图解释的一个例子:

Var
NotDone:Boolean=False;

Function MyTestFunction:Boolean;
begin

 if NotDone<>True then
 begin
   NotDone:=True;
   MyTestFunction();
 end else
 begin
   Result:=True;
 end;

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
NotDone:=False;

if mMyTestFunction=True then
begin
  ShowMessage('Returned: True');
end else
begin
   ShowMessage('Returned: False');
end;

end;

所以我基本上想调用我的函数,并在某些情况下从内部“调用”,而不是主调用(button1 click 中的代码表达式)从第一次调用中获取结果,然后处理 Second 并返回它。

如您所见,我本来希望它返回true,但它返回false。

标签: delphi

解决方案


如您所见,我本来希望它返回true,但它返回false。

实际上,返回值是未定义的,就像在“NotDone<>True”的情况下,你永远不会为调用的结果分配任何值......

如果我理解正确,这不是吗:

Function MyTestFunction:Boolean;
begin

 if NotDone<>True then
 begin
   NotDone:=True;
   Result:=MyTestFunction();  // Return the value from second invokation
 end else
 begin
   Result:=True;
 end;

end;

推荐阅读