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

image from book
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.
image from book

image from book
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.

Inside Delphi 2006
Inside Delphi 2006 (Wordware Delphi Developers Library)
ISBN: 1598220039
EAN: 2147483647
Year: 2004
Pages: 212
Authors: Ivan Hladni

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net