你想要怎么理解,自己调用自己,比如procedure Add(i:integer); begin if i < 10 then begin Add(i+1); end; end; 只要i< 10就会递归,每次把i+1作为参数传入
递归函数,可以线性方式理解为:在一个死循环中执行的一个函数。有一个条件可以决定是否跳出这个死循环。递归函数: function func1(...): boolean; begin result := func1(...); if not result then Exit; end;转换为线性方式: while true do begin if not func1(...) then break; end;
果然是要例子+解说才比较好理解,如果只是以书面解释的话,像我这样没接触过递归的就不太好理解了!按照二楼的代码,自己参考着写了一下,现在对此概念已经有初步的了解了!: //递归过程 procedure alert(i: Integer = 1); begin ShowMessage(IntToStr(i)); Inc(i); if i<10 then alert(i); end;//递归函数 function alert2(i:Integer):string; begin Form1.Text := Form1.Text + IntToStr(i) + ' '; Inc(i); if i<10 then begin alert2(i); end; Result := ''; end;procedure TForm1.Button1Click(Sender: TObject); begin alert(); {调用递归过程} end;procedure TForm1.Button2Click(Sender: TObject); begin alert2(2); {调用递归函数} end;end. 非常感谢各位,可惜分数不多了
begin
if i < 10 then
begin
Add(i+1);
end;
end;
只要i< 10就会递归,每次把i+1作为参数传入
function func1(...): boolean;
begin
result := func1(...);
if not result then
Exit;
end;转换为线性方式:
while true do
begin
if not func1(...) then
break;
end;
//递归过程
procedure alert(i: Integer = 1);
begin
ShowMessage(IntToStr(i));
Inc(i);
if i<10 then
alert(i);
end;//递归函数
function alert2(i:Integer):string;
begin
Form1.Text := Form1.Text + IntToStr(i) + ' ';
Inc(i);
if i<10 then
begin
alert2(i);
end;
Result := '';
end;procedure TForm1.Button1Click(Sender: TObject);
begin
alert(); {调用递归过程}
end;procedure TForm1.Button2Click(Sender: TObject);
begin
alert2(2); {调用递归函数}
end;end.
非常感谢各位,可惜分数不多了