以前总以为 result 可以多次赋值,运行到函数最后时退出,对函数名赋值后,函数立即退出,今晚捉虫时发现它和result是一样的,都运行到最后才返回,返回值为最后一次赋的那个值,要想提前返回必须加exit。很是晕,出了一身冷汗,想想以前写的都是result + exit返回的,要是直接对函数名赋值还以为它会提前返回,不知会有多少个虫... (高手莫笑,我以前真是这么认为的)
function aaa():integer;
var
i:integer;
begin
aaa := 10;
i := 1000;
aaa := 1000;
end;
procedure tform1.button1click(sender: tobject);
begin
edit1.text := inttostr(aaa()); // 结果是1000,以前总以为它是10呢。
end;
function aaa():integer;
var
i:integer;
begin
aaa := 10;
i := 1000;
aaa := 1000;
end;
procedure tform1.button1click(sender: tobject);
begin
edit1.text := inttostr(aaa()); // 结果是1000,以前总以为它是10呢。
end;