Quick start manual
6-8
Delphi Language Guide
Declaring procedures and functions
where the first stringConstant gives the name of the library file and the second
stringConstant is the routine’s original name.
On Windows:
The following declaration imports a function from user32.dll (part of the Windows
API).
function MessageBox(HWnd: Integer; Text, Caption: PChar; Flags: Integer): Integer;
stdcall; external 'user32.dll' name 'MessageBoxA';
The function’s original name is MessageBoxA, but it is imported as MessageBox.
Instead of a name, you can use a number to identify the routine you want to
import:
external stringConstant index integerConstant;
where integerConstant is the routine’s index in the export table.
On Linux:
The following declaration imports a standard system function from libc.so.6.
function OpenFile(const PathName: PChar; Flags: Integer): Integer; cdecl;
external 'libc.so.6' name 'open';
The function’s original name is open, but it is imported as OpenFile.
In your importing declaration, be sure to match the exact spelling and case of the
routine’s name. Later, when you call the imported routine, the name is case-
insensitive.
For more information about libraries, see Chapter 9, “Libraries and packages”.
Overloading procedures and functions
You can declare more than one routine in the same scope with the same name. This is
called overloading. Overloaded routines must be declared with the overload directive
and must have distinguishing parameter lists. For example, consider the declarations
function Divide(X, Y: Real): Real; overload;
begin
Result := X/Y;
end;
function Divide(X, Y: Integer): Integer; overload;
begin
Result := X div Y;
end;
These declarations create two functions, both called Divide, that take parameters of
different types. When you call Divide, the compiler determines which function to
invoke by looking at the actual parameters passed in the call. For example,
Divide(6.0, 3.0) calls the first Divide function, because its arguments are real-valued.