Quick start manual
Procedures and functions
6-9
Declaring procedures and functions
You can pass to an overloaded routine parameters that are not identical in type with
those in any of the routine’s declarations, but that are assignment-compatible with
the parameters in more than one declaration. This happens most frequently when a
routine is overloaded with different integer types or different real types—for
example,
procedure Store(X: Longint); overload;
procedure Store(X: Shortint); overload;
In these cases, when it is possible to do so without ambiguity, the compiler invokes
the routine whose parameters are of the type with the smallest range that
accommodates the actual parameters in the call. (Remember that real-valued
constant expressions are always of type Extended.)
Overloaded routines must be distinguished by the number of parameters they take
or the types of their parameters. Hence the following pair of declarations causes a
compilation error.
function Cap(S: string): string; overload;
ƒ
procedure Cap(var Str: string); overload;
ƒ
But the declarations
function Func(X: Real; Y: Integer): Real; overload;
ƒ
function Func(X: Integer; Y: Real): Real; overload;
ƒ
are legal.
When an overloaded routine is declared in a forward or interface declaration, the
defining declaration must repeat the routine’s parameter list.
The compiler can distinguish between overloaded functions that contain AnsiString/
PChar and WideString/WideChar parameters in the same parameter position.
String constants or literals passed into such an overload situation are translated into
the native string or character type, which is AnsiString/PChar.
procedure test(const S: String); overload;
procedure test(const W: WideString); overload;
var
a: string;
b: widestring;
begin
a := 'a';
b := 'b';
test(a); // calls String version
test(b); // calls WideString version
test('abc'); // calls String version
test(WideString('abc')); // calls widestring version
end;