数值参数:实参的值赋给对应形参,形参的改变不影响实参
unit first; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) btn1: TButton; edt1: TEdit; edt2: TEdit; procedure btn1Click(Sender: TObject); private function formalparameter(x, y: integer): integer; { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.btn1Click(Sender: TObject); var x, y: integer; r: integer; begin x := 1; y := 100; r:=formalparameter(x, y); edt1.Text:='x='+IntToStr(x); edt2.Text:='y='+IntToStr(y); MessageBox(0, PChar(IntToStr(r)), 'R的值为:', MB_OK); end; function TForm1.formalparameter(x, y: integer): integer; begin x := x + 1; y := y + 2; Result := x + y; end; end.
输出:x=1,y=100,r=104
变量参数:变量参数传递一个指向参数的引用,即指针。改变引用传递 的参数将影响对应的实际参数
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) btn1: TButton; edt1: TEdit; edt2: TEdit; procedure btn1Click(Sender: TObject); procedure variableparameter(var x, y: integer); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.btn1Click(Sender: TObject); var x,y:integer; begin x:=100; y:=100; variableparameter(x,y); edt1.Text:='x值为:'+IntToStr(x); edt2.Text:='y值为:'+IntToStr(y); end; procedure TForm1.variableparameter(var x, y: integer); begin x:=x+1; y:=y+1; end; end.
输出:x=101,y=101
常量参数:传递给函数或过程的参数在函数或过程中不能被改变,
function myfunction(const x, y: integer): Integer; begin // x := x + 1; 错误语法:不能改变参数的值 //y := y + 1; Result := x * y; end;
4. 数组参数 :数组可以作为函数或过程的参数,但在对数组参数进行声明中不能包含数组索引类型的声明。
procedure Sort(A: array[1..10] of Integer); //不正确 type TDigits = array[1..10] of Integer; procedure Sort(A: TDigits); //正确 可以使用动态数组作为函数和过程的参数。 例: procedure Clear(var A: array of Real); var I: Integer; begin for I := 0 to High(A) do A[I] := 0; end;