Normally, every function and procedure in the unit must have a unique name. However, you can declare multiple procedures and functions that have the same name if you overload them. Overloaded procedures and functions can have the same name, but they must have parameter lists that differ in the number of parameters, parameter types, or both. To overload a procedure or a function, you have to mark it with the overload directive.
Listing 5-15: Overloaded functions
program Project1; {$APPTYPE CONSOLE} uses SysUtils; function Max(X, Y: Integer): Integer; overload; begin if X > Y then Result := X else Result := Y; end; function Max(X, Y, Z: Integer): Integer; overload; begin Result := X; if Y > Result then Result := Y; if Z > Result then Result := Z; end; begin WriteLn(Max(5, 4, 3)); ReadLn; end.
Figure 5-9: Code Insight feature showing overloaded parameter lists
When you call overloaded functions, Delphi determines which one to call by the parameters you pass. If you press Ctrl+Shift+Space in the Code Editor while calling an overloaded function or procedure, Delphi shows you the parameter lists of all overloads.