递归应该如何理解,书也看了,搜索也搜了,不过还是不怎么明白在函数中调用自身!希望有空闲的朋友帮忙写一个简单的例子,越简单越容易理解为好,多谢

解决方案 »

  1.   

    你想要怎么理解,自己调用自己,比如procedure Add(i:integer);
    begin
      if i < 10 then
      begin
        Add(i+1);
      end;
    end;
    只要i< 10就会递归,每次把i+1作为参数传入
      

  2.   

    递归函数,可以线性方式理解为:在一个死循环中执行的一个函数。有一个条件可以决定是否跳出这个死循环。递归函数:
    function func1(...): boolean;
    begin
      result := func1(...);
      if not result then 
        Exit;
    end;转换为线性方式:
    while true do
    begin
      if not func1(...) then
        break;
    end;
      

  3.   

    果然是要例子+解说才比较好理解,如果只是以书面解释的话,像我这样没接触过递归的就不太好理解了!按照二楼的代码,自己参考着写了一下,现在对此概念已经有初步的了解了!:
    //递归过程
    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.
    非常感谢各位,可惜分数不多了