7.數組參數
//這樣做是錯的
procedure Sort(A: array[1..10] of Integer) // syntax error<
//這樣做是對的
type TDigits = array[1..10] of Integer;
procedure Sort(A: TDigits);
//例如
type
TDynamicArray = array of Integer;
procedure p(Value: TDynamicArray);
begin
Value[0] := 1;
end;
procedure Run;
var
a: TDynamicArray;
begin
SetLength(a, 1);
a[0] := 0;
p(a);
Writeln(a[0]); // Prints '1'
end;
//Open Array Parameters
procedure Clear(var A: array of Real);
var
I: Integer;
begin
for I := 0 to High(A) do A[I] := 0;
end;
function Sum(const A: array of Real): Real;
var
I: Integer;
S: Real;
begin
S := 0;
for I := 0 to High(A) do S := S + A[I];
Sum := S;
end;
//Variant Open Array Parameters
function MakeStr(const Args: array of const): string;
var
I: Integer;
begin
Result := '';
for I := 0 to High(Args) do
with Args[I] do
case VType of
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)默認參數的定義和使用
procedure FillArray(A: array of Integer; Value: Integer = 0);
//使用,兩種方式都可以
FillArray(MyArray);
FillArray(MyArray, 0);
2)注意
//這樣定義是錯的
function MyFunction(X, Y: Real = 3.5): Real; // syntax error
//這樣定義是對的
function MyFunction(X: Real = 3.5; Y: Real = 3.5): Real;