1. Computing

Understanding Method Overloading and Default Parameters

for Delphi beginners

By

Overloaded Functions

Functions and procedures are an important part of the Delphi language. Starting with Delphi 4, Delphi allows us to work with functions and procedures that support default parameters (making the parameters optional), and permits two or more routines to have identical name, but operate as totally different routines.

Let's see how Overloading and default parameters can help you code better...

Overloading

Simply put overloading is declaring more than one routine with the same name. Overloading allows us to have multiple routines that share the same name, but with different number of parameters and their types.

As an example, let's consider the following two functions:

 {Overloaded routines must be declared
 with the overload directive}
 
 function SumAsStr(a, b :integer): string; overload;
 begin
    Result := IntToStr(a + b) ;
 end;
 
 function SumAsStr(a, b : extended; Digits:integer): string; overload;
 begin
    Result := FloatToStrF(a + b, ffFixed, 18, Digits) ;
 end; 
These declarations create two functions, both called SumAsStr, that take different number of parameters, that are of different types. When we call an overloaded routine, the compiler must be able to tell which routine we want to call.

For example, SumAsStr(6, 3) calls the first SumAsStr function, because its arguments are integer-valued.

Note that Delphi will help you pick the right implementation with the help of code completion and code insight.

On the other hand, if we try to call the SumAsStr function as follows:

 SomeString := SumAsStr(6.0,3.0) 
we'll get the following error: "there is no overloaded version of 'SumAsStr' that can be called with these arguments", meaning that we should also include the Digits parameter used to specify the number of digits after the decimal point.

Note: there is only one rule when writing overloaded routines: an overloaded routine must differ in at least one parameter type. The return type, instead, cannot be used to distinguish among two routines.

Two units - one routine

Let's say we have one routine in unit A, and unit B uses unit A, but declares a routine with the same name. The declaration in unit B does not need the overload directive: we should use unit A's name to qualify calls to A's version of the routine from unit B. Something like:
 unit B;
 ...
 uses A;
 ...
 procedure RoutineName;
 begin
   Result := A.RoutineName;
 end; 
An alternative to using overloaded routines is to use default parameters, which usually results in less code to write and maintain.

Default / Optional Parameters

In order to simplify some statements we can give a default value for the parameter of a function or procedure, and we can call the routine with or without the parameter, making it optional. To provide a default value, end the parameter declaration with the = symbol followed by a constant expression.

For example, given the declaration

 function SumAsStr (a,b : extended; Digits : integer = 2) : string; 
the following function calls are equivalent.
 SumAsStr(6.0, 3.0) 

SumAsStr(6.0, 3.0, 2)

Note: parameters with default values must occur at the end of the parameter list, and must be passed by value or as const. A reference (var) parameter cannot have a default value.

When calling routines with more than one default parameter we cannot skip parameters (like in VB):

 function SkipDefParams(var A:string; B:integer=5, C:boolean=False):boolean;
 ...
 //this call generates an error message
 CantBe := SkipDefParams('delphi', , True) ; 

Overloading with Default Parameters

When using both function or procedure overloading and default parameters, don't introduce ambiguous routine declarations.

Consider the following declarations:

 procedure DoIt(A:extended; B:integer = 0) ; overload;
 
 procedure DoIt(A:extended) ; overload; 
The call to DoIt procedure like DoIt(5.0) does not compile. Because of the default parameter in the first procedure, this statement might call both procedures, because it is impossible to tell which procedure is meant to be called.

©2014 About.com. All rights reserved.