Delphi语言学习6-函数参数
1.参数定义方式
Code
function Power(X: Real; Y: Integer): Real;
//(X, Y: Real)
//(var S: string; X: Integer)
//(HWnd: Integer; Text, Caption: PChar; Flags: Integer)
//(const P; I: Integer)
function Power(X: Real; Y: Integer): Real;
//(X, Y: Real)
//(var S: string; X: Integer)
//(HWnd: Integer; Text, Caption: PChar; Flags: Integer)
//(const P; I: Integer)
2.引用类型和值类型
Code
//DoubleByRef 的参数值会改变
function DoubleByValue(X: Integer): Integer; // X is a value parameter
begin
X := X * 2;
Result := X;
end;
function DoubleByRef(var X: Integer): Integer; // X is a variable parameter
begin
X := X * 2;
Result := X;
end;
//DoubleByRef 的参数值会改变
function DoubleByValue(X: Integer): Integer; // X is a value parameter
begin
X := X * 2;
Result := X;
end;
function DoubleByRef(var X: Integer): Integer; // X is a variable parameter
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.常量参数
Code
function CompareStr(const S1, S2: string): Integer;
function CompareStr(const S1, S2: string): Integer;
4.输出参数
Code
procedure GetInfo(out Info: SomeRecordType);
//使用方式
var MyRecord: SomeRecordType;
GetInfo(MyRecord);
procedure GetInfo(out Info: SomeRecordType);
//使用方式
var MyRecord: SomeRecordType;
GetInfo(MyRecord);
5.非强类型参数
Code
function Equal(var Source, Dest; Size: Integer): Boolean;
type
TBytes = array[0..MaxInt - 1] of Byte;
var
N : Integer;
begin
N := 0;
while (N < Size) and (TBytes(Dest)[N] = TBytes(Source)[N]) do
Inc(N);
Equal := N = Size;
end;
//使用方法
//Given the declarations
type
TVector = array[1..10] of Integer;
TPoint = record
X, Y: Integer;
end;
var
Vec1, Vec2: TVector;
N: Integer;
P: TPoint;
//调用
Equal(Vec1, Vec2, SizeOf(TVector)); // compare Vec1 to Vec2
Equal(Vec1, Vec2, SizeOf(Integer) * N); // compare first N elements of Vec1 and Vec2
Equal(Vec1[1], Vec1[6], SizeOf(Integer) * 5); // compare first 5 to last 5 elements of Vec1
Equal(Vec1[1], P, 4); // compare Vec1[1] to P.X and Vec1[2] to P.Y
function Equal(var Source, Dest; Size: Integer): Boolean;
type
TBytes = array[0..MaxInt - 1] of Byte;
var
N : Integer;
begin
N := 0;
while (N < Size) and (TBytes(Dest)[N] = TBytes(Source)[N]) do
Inc(N);
Equal := N = Size;
end;
//使用方法
//Given the declarations
type
TVector = array[1..10] of Integer;
TPoint = record
X, Y: Integer;
end;
var
Vec1, Vec2: TVector;
N: Integer;
P: TPoint;
//调用
Equal(Vec1, Vec2, SizeOf(TVector)); // compare Vec1 to Vec2
Equal(Vec1, Vec2, SizeOf(Integer) * N); // compare first N elements of Vec1 and Vec2
Equal(Vec1[1], Vec1[6], SizeOf(Integer) * 5); // compare first 5 to last 5 elements of Vec1
Equal(Vec1[1], P, 4); // compare Vec1[1] to P.X and Vec1[2] to P.Y
6.字符串参数
Code
//这样做是错的
procedure Check(S: string[20]); // syntax error
//这样做是对的
type TString20 = string[20];
procedure Check(S: TString20);
//OpenString
procedure Check(S: OpenString);
//这样做是错的
procedure Check(S: string[20]); // syntax error
//这样做是对的
type TString20 = string[20];
procedure Check(S: TString20);
//OpenString
procedure Check(S: OpenString);
7.数组参数
Code
//这样做是错的
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])
//这样做是错的
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)默认参数的定义和使用
Code
procedure FillArray(A: array of Integer; Value: Integer = 0);
//使用,两种方式都可以
FillArray(MyArray);
FillArray(MyArray, 0);
procedure FillArray(A: array of Integer; Value: Integer = 0);
//使用,两种方式都可以
FillArray(MyArray);
FillArray(MyArray, 0);
2)注意
Code
//这样定义是错的
function MyFunction(X, Y: Real = 3.5): Real; // syntax error
//这样定义是对的
function MyFunction(X: Real = 3.5; Y: Real = 3.5): Real;
//这样定义是错的
function MyFunction(X, Y: Real = 3.5): Real; // syntax error
//这样定义是对的
function MyFunction(X: Real = 3.5; Y: Real = 3.5): Real;