看资料不是说函数名会返回吗?
function test():string;
begin
ShowMessage('1');
result:='a';
ShowMessage('2');
test:='b';//这边也不返回?
ShowMessage('3');
end;
function test():string;
begin
ShowMessage('1');
result:='a';
ShowMessage('2');
test:='b';//这边也不返回?
ShowMessage('3');
end;
begin
ShowMessage('1');
result:='a';
ShowMessage('2');
test:='b';//这边也不返回?
ShowMessage('3');
end;
你这个函数只会返回a
begin
result:='a';
end;
procedure TForm1.Button17Click(Sender: TObject);
var
s:string;
begin
s:=test;
end;
你这个函数返回就是 a啊 result:='a';
至于 test:='b';就看不懂了,
要不就是给 test变量赋值
begin
result := 'sss';
test := 'sfdsf';
end;
返回的是'sfdsf'
procedure TForm1.BitBtn3Click(Sender: TObject);
function test(): string;
begin
ShowMessage('1');
result := 'a';
ShowMessage('2');
test := 'b'; //这边也不返回?
ShowMessage('3');
end;
begin
ShowMessage(test); //此时显示 'b'
end;
函数名 := myresult;
或
result := myresult;
这样的语句处返回,而是顺序执行下去,除非碰到
exit; abort; halt;之类的显示语句而c/c++会在
return; return 10;这样的句子处返回调用点,而不执行函数在return后边的语句
是啊 我就这个意思
我看到很多地方都说 result不返回,函数名 可以返回 以为函数名 可以不加exit;
相当于
result := 'b'; exit;
begin
ShowMessage('1');
result:='a';
ShowMessage('2');
test:='b';//这边也不返回?
ShowMessage('3');
end;
没有OK确认函数就会暂停,也就是还没有执行结束,那来的返回值
所以最后返回值就是b只要记住,函数返回值的是最后一个运行的result (或者函数名)
都只是准备返回值,但是不是马上返回使用函数名不如result,因为万一以后函数名因为重构变化了,函数内部所以的 函数名:=xxx 都需要修改
result就没有这个问题另外,如果 a:=函数名 也有歧义,是:直接使用返回值,还是 递归执行函数再获得返回值呢?
if yyyy then
begin
result:=xxx;
exit;
end;
不如c的:
if (yyyy)
return xxx;
简洁