O
only you
Unregistered / Unconfirmed
GUEST, unregistred user!
请看下面的代码段:
问题是,函数中的变量在下次进入此函数时会丢失先前的值,但返回值却保持上一次的值,为什么?
function TestVariable(var i:Integer):String;
var
S:String;
begin
if I=0 then
S:='OK';
Result:=S;
end;
function TestResult(var i:Integer):string;
var
k:integer;
begin
if i=0 then
Result:='K='+InttoStr(K)+' OK';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to 3 do
begin
Showmessage(TestResult(i));
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to 3 do
begin
Showmessage(TestVariable(i));
end;
end;
问题是,函数中的变量在下次进入此函数时会丢失先前的值,但返回值却保持上一次的值,为什么?
function TestVariable(var i:Integer):String;
var
S:String;
begin
if I=0 then
S:='OK';
Result:=S;
end;
function TestResult(var i:Integer):string;
var
k:integer;
begin
if i=0 then
Result:='K='+InttoStr(K)+' OK';
end;
procedure TForm1.Button1Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to 3 do
begin
Showmessage(TestResult(i));
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to 3 do
begin
Showmessage(TestVariable(i));
end;
end;