1.參數定義方式
functionPower(X:Real;Y:Integer):Real;
//(X,Y:Real)
//(varS:string;X:Integer)
//(HWnd:Integer;Text,Caption:PChar;Flags:Integer)
//(constP;I:Integer)
2.引用類型和值類型
//DoubleByRef的參數值會改變
functionDoubleByValue(X:Integer):Integer; //Xisavalueparameter
begin
X:=X*2;
Result:=X;
end;
functionDoubleByRef(varX:Integer):Integer; //Xisavariableparameter
begin
X:=X*2;
Result:=X;
end;
var
I, J, V, W: Integer;
begin
I := 4;
V := 4;
J := DoubleByValue(I); // J = 8, I = 4
W := DoubleByRef(V); // W = 8, V = 8
end;
3.常量參數
functionCompareStr(constS1,S2:string):Integer;
4.輸出參數
procedureGetInfo(outInfo:SomeRecordType);
//使用方式
varMyRecord:SomeRecordType;
……
GetInfo(MyRecord);
5.非強類型參數
functionEqual(varSource,Dest;Size:Integer):Boolean;
type
TBytes=array[0..MaxInt-1]ofByte;
var
N:Integer;
begin
N:=0;
while(N<Size)and(TBytes(Dest)[N]=TBytes(Source)[N])do
Inc(N);
Equal:=N=Size;
end;
//使用方法
//Giventhedeclarations
type
TVector=array[1..10]ofInteger;
TPoint=record
X,Y:Integer;
end;
var
Vec1,Vec2:TVector;
N:Integer;
P:TPoint;
//調用
Equal(Vec1,Vec2,SizeOf(TVector)); //compareVec1toVec2
Equal(Vec1,Vec2,SizeOf(Integer)*N); //comparefirstNelementsofVec1andVec2
Equal(Vec1[1],Vec1[6],SizeOf(Integer)*5); //comparefirst5tolast5elementsofVec1
Equal(Vec1[1],P,4); //compareVec1[1]toP.XandVec1[2]toP.Y
6.字符串參數
//這樣做是錯的
procedureCheck(S:string[20]); //syntaxerror
//這樣做是對的
typeTString20=string[20];
procedureCheck(S:TString20);
//OpenString
procedureCheck(S:OpenString);
7.數組參數
//這樣做是錯的
procedureSort(A:array[1..10]ofInteger) //syntaxerror<
//這樣做是對的
typeTDigits=array[1..10]ofInteger;
procedureSort(A:TDigits);
//例如
type
TDynamicArray=arrayofInteger;
procedurep(Value:TDynamicArray);
begin
Value[0]:=1;
end;
procedureRun;
var
a:TDynamicArray;
begin
SetLength(a,1);
a[0]:=0;
p(a);
Writeln(a[0]);//Prints'1'
end;
//OpenArrayParameters
procedureClear(varA:arrayofReal);
var
I:Integer;
begin
forI:=0toHigh(A)doA[I]:=0;
end;
functionSum(constA:arrayofReal):Real;
var
I:Integer;
S:Real;
begin
S:=0;
forI:=0toHigh(A)DOS:=S+A[I];
Sum:=S;
end;
//VariantOpenArrayParameters
functionMakeStr(constArgs:arrayofconst):string;
var
I:Integer;
begin
Result:='';
forI:=0toHigh(Args)do
withArgs[I]do
caseVTypeof
vtInteger: Result:=Result+IntToStr(VInteger);
vtBoolean: Result:=Result+BoolToStr(VBoolean);
vtChar: Result:=Result+VChar;
vtExtended:Result:=Result+FloatToStr(VExtended^);
vtString: Result:=Result+VString^;
vtPChar: Result:=Result+VPChar;
vtObject: Result:=Result+VObject.ClassName;
vtClass: Result:=Result+VClass.ClassName;
vtAnsiString: Result:=Result+string(VAnsiString);
vtCurrency: Result:=Result+CurrToStr(VCurrency^);
vtVariant: Result:=Result+string(VVariant^);
vtInt64: Result:=Result+IntToStr(VInt64^);
end;
end;
//調用方式
MakeStr(['test',100,'',True,3.14159,TForm])
8.默認參數
1)默認參數的定義和使用
procedureFillArray(A:arrayofInteger;Value:Integer=0);
//使用,兩種方式都可以
FillArray(MyArray);
FillArray(MyArray,0);
2)注意
//這樣定義是錯的
functionMyFunction(X,Y:Real=3.5):Real; //syntaxerror
//這樣定義是對的
functionMyFunction(X:Real=3.5;Y:Real=3.5):Real;