Delphi Method Overloading and Default Parameters

How Overloading & Default Parameters Work in Delphi

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 an identical name but operate as completely 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 a different number of parameters and 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 a different number of parameters and are of two 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: Delphi will help you pick the right implementation with the help of code completion and code insight.

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

 SomeString := SumAsStr(6.0,3.0) 

We'll get an error that reads: "there is no overloaded version of 'SumAsStr' that can be called with these arguments." This means 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, and that is that 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.

Consider something like this:

 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 equal (=) 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.

Format
mla apa chicago
Your Citation
Gajic, Zarko. "Delphi Method Overloading and Default Parameters." ThoughtCo, Aug. 25, 2020, thoughtco.com/understanding-method-overloading-and-default-parameters-1058217. Gajic, Zarko. (2020, August 25). Delphi Method Overloading and Default Parameters. Retrieved from https://www.thoughtco.com/understanding-method-overloading-and-default-parameters-1058217 Gajic, Zarko. "Delphi Method Overloading and Default Parameters." ThoughtCo. https://www.thoughtco.com/understanding-method-overloading-and-default-parameters-1058217 (accessed March 19, 2024).