设置一个全局变量Y:Boolean.
function MyFun(sStr:String):String;
var ss: String;
    ii: Integer; 
begin
  if Y then exit;
  ........................
  while ii < 100 do
  begin
    MyFun(ss);
  end;
  ........................
end;然后当你要中断递归的时候就把Y设置为True就好了。

解决方案 »

  1.   

    我的意思是:
    function MyFun(sStr:String):String;
    var ss: String;
        ii: Integer; 
    begin
      if Y then exit;
      ........................
      while ii < 100 do
      begin
        if ss > IntToStr(ss) then
          中断; 
        MyFun(ss);
      end;
      ........................
    end;
      

  2.   

    var
      Y: Boolean;function MyFun(sStr:String):String;
    var ss: String;
        ii: Integer; 
    begin
      if Y then exit;
      ........................
      while ii < 100 do
      begin
        if ss > IntToStr(ss) then begin
          Y := True;
          Exit;
        end;
        MyFun(ss);
      end;
      ........................
    end;
      

  3.   

    function MyFun(sStr:String):String;
    var ss: String;
        ii: Integer; 
    begin
      if Y then exit;
      ........................
      while ii < 100 do
      begin
        if ss > IntToStr(ss) then
          中断; 
        MyFun(ss);
        if Y then exit; // :)
      end;
      ........................
    end;
      

  4.   

    下面的这各个例子是可以自己退出的。
    你参考一下吧。
    function myfunc(k:integer):integer;
    begin
      if k = 1 then result := 1
      else result := k + myfunc(k-1)
    end;
      

  5.   

    function MyFun(sStr:String):String;
    var ss: String;
        ii: Integer; 
    begin
      if Y then exit;
      ........................
      while ii < 100 do
      begin
        if ss > IntToStr(ss) then
          中断; 
        MyFun(ss);
        if Y then exit; // :)
      end;
      ........................
    end;
      

  6.   

    function MyFun(sStr:String):String;
    var ss: String;
        ii: Integer; 
    begin
      ........................
      while ii < 100 do
      begin
        if ss > IntToStr(ss) then
         exit;// 中断; 
        MyFun(ss);
      end;
      ........................
    end;
      

  7.   

    N的阶乘:
    function GetFactorial(N: Integer): Integer;
    begin
      if N <= 1 then Result := 1
      else Result := N * GetFactorial(N-1);
    end;
    是这样吗?function MyFun(sStr:String):String;
    var ss: String;
        ii: Integer; 
    begin
      ........................
      ss := sStr;
      while ii < 100 do
      begin
        if ii > IntToStr(ss) then
        begin
         result := ss;// 中断; 
         exit;
        end;
        ss := MyFun(ss);
      end;
      result := ss;
      ........................
    end;
      

  8.   

    //查找后缀名匹配的第一个文件
    //已经成功
    function FindFile(sExt: String; sPath:String='C:'):String;
    var
      fpath: String;
      fs: TSearchRec;
      Status: Integer;
    begin
      fpath:=sPath+'\*.*';
      Status := FindFirst(fpath,faAnyFile,fs);
      if Status = 0 then
      begin
        if (fs.Name<>'.')and(fs.Name<>'..') then
        if (fs.Attr and faDirectory)=faDirectory then
          Result := FindFile(sExt,sPath+'\'+fs.Name)
        else
        begin
          if UpperCase(ExtractFileExt(fs.Name)) = UpperCase(sExt) then
          begin
            Result := sPath+'\'+fs.Name;
            FindClose(fs);
            Exit;
          end;
        end;
        while FindNext(fs)=0 do
        begin
          if (fs.Name<>'.')and(fs.Name<>'..') then
          if (fs.Attr and faDirectory)=faDirectory then
          begin
            Result := FindFile(sExt,spath+'\'+fs.Name);
          end
          else
          begin
            if UpperCase(ExtractFileExt(fs.Name)) = UpperCase(sExt) then
            begin
              Result := sPath+'\'+fs.Name;
              FindClose(fs);
              Exit;
            end;
          end;
        end;
      end;
      FindClose(fs);
    end;
      

  9.   

    //这样不正确,无限循环
    function GetFactorial(N: Integer): Integer;
    begin
      Result := N * GetFactorial(N-1);
    end;//这样不正确,当N<=0,永远不执行if N=1分支,将无限循环
    function GetFactorial(N: Integer): Integer;
    begin
      if N = 1 then Result := 1
      else Result := N * GetFactorial(N-1);
    end;//这样才正确
    function GetFactorial(N: Integer): Integer;
    begin
      if N <= 1 then Result := 1
      else Result := N * GetFactorial(N-1);
    end;