以下列子 它们有什么分别,功用,效果和副作用?function Double(Value : integer) : Integer;  <--这参数应该称什么?
begin
  Double := Value*2;
end;procedure TForm1.Button1Click(Sender: TObject);
var
  x : Integer;
begin
  x := Double(StrToInt(Edit1.Text));
  Label1.Caption := IntToStr(x);
end;function vDouble(var Value : Integer) : Integer; <--引用参数
begin
  vDouble := Value*2;
end;procedure TForm1.Button2Click(Sender: TObject);
var
  x : Integer;
begin
  x := StrToInt(Edit1.Text);
  x := vDouble(x);
  Label1.Caption := IntToStr(x);
  Label2.Caption := 'This is no Different to Button3.';
end;function cDouble(const Value : Integer) : Integer; <--常量参数
begin
cDouble := Value*2;
end;procedure TForm1.Button4Click(Sender: TObject);
var
x : Integer;
begin
x := StrToInt(Edit1.Text);
  x := cDouble(x);
  Label1.Caption := IntToStr(x);
  Label2.Caption := 'Similar like Button1';
end;

解决方案 »

  1.   

    第一个,对于值类型,无法修改参数值
    第二个,可以修改参数的值
    第三个,加上const就是希望参数不被修改
      

  2.   

    LZ测一下这句就明白了function vDouble(var Value : Integer) : Integer; <--引用参数
    begin
       Value := Value*2;
    end;
      

  3.   

    Lz举的例子不好,以下函数执行下立刻就知道了注意函数外的 Value 值
    function vDouble(var Value : Integer) : Integer; <--引用参数
    begin
       Value := Value*2;
    end;function Double(Value : integer) : Integer; <--这参数应该称什么?
    begin
       Value := Value*2;
    end;function cDouble(const Value : Integer) : Integer; <--常量参数
    begin
     Value := Value*2;
    end;
      

  4.   


    这个正解,
    带var 是引用,可以改变其值,方法体内与体外是一致的。
    带const 是长量参数,不允许改变其值,在方法体内不能给这个参数设置值。
    不带任何修饰的,在方法体内可以给其设置值,作用范围只能在方法体内,在方法体外仍然是传进来的值。
      

  5.   

    唉,深不深的不要紧,自己动手测一下,delphi不会咬你的
      

  6.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}function Double_A(Value:integer):Integer;//不允许缺参数调用、参数能被改变
    begin
      Value:=Value+10;
      Result:=Value;
    end;function Double_B(var Value:integer):Integer;//不允许缺参数调用、参数必须是变量、参数能被改变
    begin
      Value:=Value+20;
      Result:=Value;
    end;function Double_C(const Value:integer=30):Integer;//允许不带参数调用(无参数调用时,使用30作传入值)、参数不能被改变
    begin
    //Value:=Value+10;//因为参数是常量,因此不能赋值给Value
      Result:=Value+30;
    end;function Double_D(const Value:integer):Integer;//不允许缺参数调用、参数不能被改变
    begin
    //Value:=Value+10;//因为参数是常量,因此不能赋值给Value
      Result:=Value+40;
    end;procedure TForm1.Button1Click(Sender: TObject);
    var x:integer;
    begin
      x:=10;  //Double_A 函数调用形式:
      showmessage('Double_A函数(用常量作参数)传回的值是:'+inttostr(Double_A(10)));
      showmessage('Double_A函数(用变量作参数)传回的值是:'+inttostr(Double_A(x)));
    //showmessage('Double_A函数(无参数)传回的值是:'+inttostr(Double_A));//不能无参数  //Double_B 函数调用形式:
    //showmessage('Double_B函数(用常量作参数)传回的值是:'+inttostr(Double_B(10)));//不能以常量作参数
      showmessage('Double_B函数(用变量作参数)传回的值是:'+inttostr(Double_B(x)));
      //showmessage('Double_B函数(无参数)传回的值是:'+inttostr(Double_B));//不能无参数  //Double_C 函数调用形式:
      showmessage('Double_C函数(用常量作参数)传回的值是:'+inttostr(Double_C(10)));
      showmessage('Double_C函数(用变量作参数)传回的值是:'+inttostr(Double_C(x)));
      showmessage('Double_C函数(无参数)传回的值是:'+inttostr(Double_C));  //Double_D 函数调用形式:
      showmessage('Double_D函数(用常量作参数)传回的值是:'+inttostr(Double_D(10)));
      showmessage('Double_D函数(用变量作参数)传回的值是:'+inttostr(Double_D(x)));
    //showmessage('Double_D函数(无参数)传回的值是:'+inttostr(Double_D));//不能无参数
    end;end.
      

  7.   

    补充:仅 Double_B 函数对参数的值的改变,会引起传入的变量同时被改变;其余的函数仅在函数体内变化,不会引起外部的变化。
      

  8.   

    调用
     ss:='A';
     x(ss);
     
    1:无修饰,s值变成了B,但ss还是A 
    procedure x(s:string);
    begin
      s:='B';
    end;2.var修饰,s值变成了B,ss也变成B
    procedure x(var s:string);
    begin
      s:='B';
    end;3.Const修饰,s是传入什么,就是什么,不能改变
    procedure x(Const s:string);
    begin
      //s:='B'; 这里不能再重新赋值
    end;
      

  9.   

    一、实参类型与形参类型不匹配时,实参类型自动转化为形参类型Return 语句 返回数据的类型 与 函数类型 不匹配时,自动转化为函数的类型         函数声明:                   事先通知编译系统,可以不写形参名。                   float  add(float, float);二、Skills编程时把main函数写到最前,每次需调用函数时都要写 函数原型 来声明函数。例如如此声明函数: void swap(int, int);三、关于参数的类型    指针作为函数参数                          声明时:void swap(int *p1, int *p2);                                               调用时: swap(pointer_1, pointer_2);    一维数组地址 作函数参数,用指针变量去接受或者用数组接收                       声明时:void swap(int a[]);或者 void swap(int *a);                                                 调用时: swap(a);   //a为数组名    多维数组名作函数参数时,用指向一维数组的指针变量接收或多维数组接受              声明时:void swap(int a[ ][n ]);或者 void swap( int (*p) [ n] );                                                  //n为具体数组的列数,必须要指定              调用时: swap(a);     //a为二维数组名                                       二维数组a[5][6]; 其中 a == &a[0]  a[0] == &a[0][0]                                       a[1] == &a[1][0]  a[2] == &a[2][0] ……  //原则:数组名为首元                                       地址 a 和 a[n],(n=1,2,……) 同样适用。    函数指针作参数                 声明时:void swap(double (*p)(int,int) );                                               调用时: swap(f); //只需传参数名(函数入口地址),不需                                                                  //传函数的参数。其中f为函数名    p188 /c++ /谭                                               区别以下两者的差别:                                                        void *swap(int,int);                                                        void (*swap)(int,int);                                                        前者是声明一个函数swap,返回值为指向void类型的指针                                                         后者是声明一个指向函数的指针,这个指针是 swap                      //参考p187/c++谭    引用作为函数参数                  声明时:void swap(int &, int &);  //参考p194/c++谭                                               调用时: swap(i, j);    //i,j可以是变量也可以是引用类型    结构体类型变量作参数                  声明时:void swap(struct student stu1);                                                调用时: swap(stu);//stu是结构体student的一个变量    类对象作参数                  声明时:void swap(student stu1);//student是一个以定义的类                                                调用时: swap(stu); //stu是类student 一个对象四、定义字符指针            Char *str = “I Love China”;  //str是指针,系统将首元素I的地址