function Amax(var a:array of double):double;
var i:integer;
    ma:double;
begin
   result:=a[low(a)];
   for i := low(a) to high(a) do
   begin
     if a[i]>=ma then
       result:=a[i];
   end;end;
procedure TForm1.Button1Click(Sender: TObject);
var a:array[1..10] of double;
    i:integer;
begin
     for i := 1 to 10 do
       a[i]:=i;
       a[2]:=198;
     showmessage(floattostr(amax(a)));   现实的是10,为什么不是198呢
end;

解决方案 »

  1.   

    function Amax(var a:array of double):double;
    函数中ma没有赋初值
      

  2.   

    Amax函数悲剧了。
    a[i]>=result
      

  3.   

    给Result赋值函数不会直接返回的,if a[i]>result then.....
      

  4.   

    function Amax(var a:array of double):double;
    var i:integer;
      ma:double;
    begin
      ma:=a[low(a)];
      for i := low(a)+1 to high(a) do
      begin
      if a[i]>ma then
       ma:=a[i];
      end;
      Result:=ma;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    var a:array[1..10] of double;
      i:integer;
    begin
      for i := 1 to 10 do
      a[i]:=i;
      a[2]:=198;
      showmessage(floattostr(amax(a)));//绝对返回198
    end;