The way to create a procedure with default values is as follows:
Procedure Name(Var1 : Type1; Var2 : Type2 = Value2);
This procedure can be called in either of the following ways
Name(MyValue1);
or
Name(MyValue1,MyValue2);
The last parameters are the ones that can have default values you obviously cannot do the following:
Procedure Name(Var1 : Type1 = Value1; Var2 : Type2);
It is also possible to create the same procedure with different parameter lists (Overloaded procedures).
Here is a set of examples:
Function FullName(First : String; Second : String = 'Unknown') : String;
Begin
If Second = 'Unknown' then
Result := First
Else
Result := First+' '+Second;
End;
Function MakeName(FirstName : String) : String; Overload;
Begin
Result := FirstName;
End;
Function MakeName(FirstName, Surname : String) : String; Overload;
Begin
Result := FirstName+' '+Surname;
End;
Function MakeName(ID : Integer) : String; Overload;
Begin
Result := IntToStr(ID);;
End;
And to use them:
ShowMessage(FullName('William'));
ShowMessage(FullName('William','Cairns'));
ShowMessage(MakeName('William'));
ShowMessage(MakeName('William','Cairns'));
ShowMessage(MakeName(1234567890));
(Tested in Delphi 5)
Bookmarks