function
The Function keyword defines a subroutine that returns a value.
Syntax 1:
function myFunc:Return Type; Directives;
Defines a function that simply returns a data type. Only one value can
be returned from a function. If you need to return multiple values you
can return an array or record structure.
Syntax 2:
function myFunc(Parameters):Return Type; Directives;
Defines a function that is passed one or more parameters, and returns a
data type. Only one value can be returned from a function. However,
besides using an array type or record structure you can use the out or
var keyword before a parameter allowing the parameter to be treated as
variable which will also contain return values.
In both cases, the returned value is passed by assigning to the Result
pseudo variable. {$MODE DELPHI} creates this variable for you at the
function start, with the correct return data type. Otherwise the return
value is assigned to a variable with the same name as the function.
When a function is defined in a class, it is commonly called a Method.
The same name may be used for more than one function as long as the Overload
directive is used. The other main directives, in the order that they should
appear is given here:
Reintroduce: Redefines a suppressed function
Overload: Allows same name for 2 or more functions
Virtual: Can be redefined in a child class
Override: Redefines a parent class method
Abstract: Forces child class to implement
Syntax 3:
type function myFunc(Parameters):Return Type; of object;
Defines a function as a data type. This allows the function to be passed as a
parameter, and used as a variable. The type definition defines just the profile
of the function - and not the name.
A variable of such a type could be assigned the name of any function with that
profile. When assigned, the variable name can be treated as if it were a function
name.
Further still, the Of Object option allows you to refer to an object method.
Access to a variable of such type would then behave as if you were calling the
object method directly.
Example
Download This Source for Free Pascal
{$MODE DELPHI} // Enable Class keyword
type
// Define a simple class
TSimple = class
private
name : string;
public
function GetName : string;
constructor Create(name : string);
end;
TNameFunc = Function : string of object;
var
simple : TSimple;
nameFunc : TNameFunc;
Function GetSum(a, b : Integer) : Integer;
begin
// Add the two numbers together, and return this value
Result := a + b;
end;
// Create a simple object
constructor TSimple.Create(name: string);
begin
// Save the passed string
self.name := name;
end;
// Returns the simple name
function TSimple.GetName: string;
begin
Result := name;
end;
begin
Writeln('1 + 2',GetSum(1,2));
Writeln('62 + 444',GetSum(62,444));
// Create a simple object
simple := TSimple.Create('Brian');
// Show the object name
Writeln('Name accessed directly = ',simple.GetName);
// Now refer to this method indirectly
nameFunc := simple.GetName;
// Show the object name
Writeln('Name accessed indirectly = ',nameFunc);
end.
Output
1 + 23
62 + 444506
Name accessed directly = Brian
Name accessed indirectly = Brian
See Also
Abstract,
Const,
Dynamic,
Out,
Overload,
Override,
Procedure,
Reintroduce,
Result,
Var,
Virtual.