IT SOLUTIONS
Your full service technology partner! 
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

January Edition
Subscribe now! It's Free!
Enter your email:

   ► KBProgrammingDelphi for W...Language Det...   Print This     
  From the July 2015 Issue of Prestwood eMag
 
Delphi Language Details:
Delphi Overloading (overload)
 
Posted 11 years ago on 10/27/2008 and updated 6/10/2009
Delphi Code Snippet:
 A flashcard from our Delphi Flashcards Library
 A code snippet from our Delphi Code Snippets Page
 Tags: Delphi , Overloading

KB101463



General Info: Overloading

Types of overloading include method overloading and operator overloading.

Method Overloading is where different functions with the same name are invoked based on the data types of the parameters passed or the number of parameters. Method overloading is a type of polymorphism and is also known as Parametric Polymorphism.

Operater Overloading allows an operator to behave differently based on the types of values used. For example, in some languages the + operator is used both to add numbers and to concatenate strings. Custom operator overloading is sometimes referred to as ad-hoc polymorphism.

Delphi Overloading

Delphi supports both method and operator overloading.

For method overloading, you use the overload keyword (all versions of the routine must include the overload keyword). The compiler chooses the correct method first based on the number of parameters, then on the type so you can have two overloaded methods with the same number of parameters so long as at least one parameter is different.

Another form of method overloading is with the use of default parameters, a shortcut syntax, where you specify a default parameter value.

Delphi also supports operator overloading with some operators.

Syntax Example:
function Add(a, b: integer): Integer;  overload;
begin
Result := a+b;
end;
function Add(const msg: String; a, b: integer): String; overload;
begin
  Result := msg + IntToStr(a+b);
end;

Default Parameters and Overloading

Delphi introduced default parameters with Delphi 4. Default parameters is a form of overloading that allows you to specify a default for a parameter which is used when the routine is called without that particular parameter.

For example, the following function demonstrates default parameter overloading. It allows you to add up to four numbers:

function Add(a, b: integer; c:integer=0; d:integer=0): Integer;
begin
Result := a+b+c+d;
end;

To test our new function, add the following code to a button click event:

ShowMessage('2+2=' + IntToStr(Add(2,2)));
ShowMessage('2+2+3=' + IntToStr(Add(2,2,3)));
ShowMessage('2+2+3+4=' + IntToStr(Add(2,2,3,4)));

Delphi 2009 Working Example

Place a button on a form and add the following functions and the procedure.

//First simple method adds 2 integers.
function Add(a, b: integer): Integer;  overload;
begin
  Result := a+b;
end;
//Same but allows strings and returns a string.
function Add(a, b: String): String;  overload;
begin
  Result := IntToStr(StrToInt(a) + StrToInt(b));
end;
//Overload with an extra string parameter
//and now Add returns a string in this form!
function Add(const msg: String; a, b: integer): String; overload;
begin
  Result := msg + IntToStr(a+b);
end;
//This function adds a 3rd and 4th
//optional parameter.
function Add(const msg: String; a, b, c: integer; d:integer=0): String; overload;
begin
  Result := msg + IntToStr(a+b+c+d);
end;
//You can even overload a function with
//a procedure (or vice versa).
procedure Add(a: integer; b:string); overload;
begin
  ShowMessage(IntToStr(a) + '+' + b + '=' + IntToStr(a + StrToInt(b)));
end;

Now test out our overloaded Add routine (it's a routine because it is both a function and procedure!).  Add the following code to a buton click event:

//2 integer parameters, returns an integer.
ShowMessage('2+2=' + IntToStr(Add(2,2)));
//2 string parameters, returns a string.
ShowMessage('3+3=' + Add('3', '3'));
//a string parameter and 2 integer parameters, returns a string.
ShowMessage(Add('4+4=', 4, 4));
//a string parameter and 3 integer parameters, returns a string.
ShowMessage(Add('2+2+3=', 2, 2, 3));
//a string parameter and 4 integer paremeters, returns a string.
ShowMessage(Add('2+2+3+4=', 2, 2, 3, 4));
//Use the procedure version.
Add(3, '4');

More Info

Definition:  Overloading

Comments

0 Comments.
Share a thought or comment...
 
Write a Comment...
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1197A1
Enter key:
Code Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains play-drums.com and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile

 KB Article #101463 Counter
21096
Since 10/27/2008
-
   Contact Us!
 
Have a question? Need our services? Contact us now.
--Mike Prestwood

Call: 916-726-5675

email: info@prestwood.com


Go ahead!   Use Us! Call: 916-726-5675 


©1995-2019 Prestwood IT Solutions.   [Security & Privacy]