1.在用自定义函数时(function),可以返回2个以上的返回值吗,谢谢.

function test(x: String;): Extended;
var
 a,b :Extended;
begin
  if x='ok' then 
  begin
    a := 10;
    b := 20;
  end
  else
  begin
    a := 20;
    b := 30;
  end;
//只能返回
  Result := 
end;但我想分别返回a和b的值.2.看其他的编程工具貌似可以返回数组,不知道delphi可以返回吗?

解决方案 »

  1.   

    可以定义一个record
    aa = record
     a:Extended;
     b:Extended;
    end;function test(x: String;): aa ;
      

  2.   

    还可以用参数传址....procedure Test(VAR a,b,c,d : integer);
    begin
      a:=2;
      b:=3;
      //....在过程中对参数的操作将直接影响a b c d
    end;procedure Button1onclick(sender:tobject);
    var
     a,b,c,d : integer;
    begin
      test(a,b,c,d);
    end;
      

  3.   

    function farray(...): array of integer;
    var
      fa:array[0..5] of integer;
    begin
      ...
      result := fa;
    end;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    type NewRet=array[0..5] of integer;
    function farray(...): NewRet;
    var
      fa:NewRet;
    begin
      ...
      fa[0] := ...;
      ...
      result := fa;
    end;
      

  4.   

    还是没做出来
    我还没掌握其中的一些用法,我的想法是通过1个简单的实例来试试例
    type NewRet=array[0..5] of integer;<---在那里写?//先定义函数
    function farray(xx: String): NewRet;
    var
      fa:NewRet;
    begin
       if xx='ok' then
      begin
        fa[0] := 10;
        fa[1] := 20;
      end
      else
      begin
        fa[0] := 30;
        fa[1] := 40;
      end;
      result := fa;
    end;//通过输入 Edit1.text 获得 XX 的值//用按键触发
    procedure TForm1.Button4Click(Sender: TObject);
    begin
      Label1.Caption := inttostr(farray(Edit1.Text));<---应该怎么写?
      Label2.Caption := inttostr(farray(Edit1.Text));<---应该怎么写?
    end;预期 Label1 和 Label2 能显示出 10和20 或 30和40.
      

  5.   

    type
      NewRet = array[0..5] of integer;function farray(xx: String): NewRet;
    begin
       if xx='ok' then
      begin
        Result[0] := 10;
        Result[1] := 20;
      end
      else
      begin
        Result[0] := 30;
        Result[1] := 40;
      end;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      fa : NewRet;
    begin
      fa := farray('ok');
      Label1.Caption := IntToStr(fa[0]);
      Label2.Caption := IntToStr(fa[1]);
    end;
      

  6.   

    这次用  lihuasoft(学习低调做人)  的方法试试思路是 a b 是条件 c d 是结果
    通过 a和b的输入, c d 赋空值
    经过 procedure 影响 c d 取c d的值来用.
      

  7.   

    啊 jadeluo(秀峰) 在线啊 酷 马上再试试
      

  8.   

    感谢 jadeluo(秀峰) !!ps:请问是怎么加分的?
      

  9.   

    可以在参数中定义两个输出参数实现你要的结果。
    function Test(x: String; out a, b: Integer): Boolean;
    begin
        Result := SameText(x, 'ok');
        if Result then   
        begin 
            a   :=   10; 
            b   :=   20; 
        end 
        else 
        begin 
            a   :=   20; 
            b   :=   30; 
        end; end;