定义
private
    { Private declarations }
    procedure  GetCharCount2(s:string;var digti,alpha,other:integer);
public   
    { Public declarations }
end;procedure TForm1.GetCharCount2(s: string; var digti, alpha, other: integer);定义
private
    { Private declarations }
    procedure  GetCharCount2(s:string;digti:integer;alpha:integer;other:integer);
public   
    { Public declarations }
end;procedure TForm1.GetCharCount2(s: string;  digti, alpha, other: integer);
两个方法怎么就不同了?

解决方案 »

  1.   

    一个小例子
    procedure TForm11.btn1Click(Sender: TObject);
    var
      s : string;
      procedure a(Value : string); //Value在方法内可修改, 不影响方法外的变量
      begin
        Value := Value + 'a';
        ShowMessage(Value);
      end;
      procedure b(var Value : string); //Value在方法内可修改, 影响方法外的变量
      begin
        Value := Value + 'b';
        ShowMessage(Value);
      end;
      procedure c(const Value : string);  //Value在方法内不能修改, 不影响方法外的变量
      begin
    //    Value := Value + 'a';  //这句编译时会提异常
        ShowMessage(Value);
      end;
    begin
      s := '123';
      a(s);
      ShowMessage(s);  s := '123';
      b(s);
      ShowMessage(s);  s := '123';
      c(s);
      ShowMessage(s);
    end;
      

  2.   

    加上var后,如果在函数内部参数值有变动,函数调用结束后参数的变动会保持,如果不加,出了这个函数,参数还是原来的值
      

  3.   

    var是引用传递,什么都不加的话是值传递
      

  4.   

    procedure Tform1.GetCharCount( S : string; var nCount_Digit, nCount_Alpha, nCount_Other : Integer );
    var
      i : Integer;
    begin
      nCount_Digit := 0;
      nCount_Alpha := 0;
      nCount_Other := 0;
      for i := 1 to Length( S ) do
        case S[i] of
          '0'..'9': Inc( nCount_Digit );
          'A'..'Z', 'a'..'z': Inc( nCount_Alpha );
          else Inc( nCount_Other );
        end;
    end;procedure TForm1.GetCharCount2(s: string; digti, alpha, other: integer);
    var i:integer;
    begin
       digti:=0;
       alpha:=0;
       other:=0;
       for i:=1 to length(s) do
       case s[i] of
       '0'..'9':inc(digti);
       'A'..'Z','a'..'z':inc(alpha);
        else
        inc(other);
       end;
     end; GetCharCount( '12///34bbb5@abcde', n1, n2, n3 ); 结果是:数字5字母8其他4
     GetCharCount2( '12///34bbb5@abcde', n1, n2, n3 );结果是:数字12161188字母4662920其他:4662920
    为啥会这样?
      

  5.   

    procedure TForm1.GetCharCount2(s: string; digti, alpha, other: integer);
    参数是值类型的, 内部可以变化, 但无法反馈到外部
    2楼有例子