Functions
Usage
Parameters (arguments)
- Value: Parameters are passed by value by default.
- Reference: To pass a parameter by reference, precede the parameter by the keyword "var" in the formal parameter list.
- Constant: To pass a constant paramter, precede the parameter by the keyword "const" in the formal parameter list.
- If a procedure or function has no parameters, the parentheses for the parameter-list are omitted - in the declaration, in the definition, and when the procedure or function is called.
- The keyword "exit" causes the program to return from the current procedure or function, before the end of the procedure or function is reached.
- Declarations are made in the "interface" section of a unit (source-code file).
- Definitions are made in the "implementation" section of a unit (source-code file).
SyntaxFunctions
DeclarationExamples
Definition
- procedure proc-name;
- procedure proc-name(param: TYPE; var param: TYPE);
procedure proc-name(param: TYPE; var param: TYPE);
var
arg: TYPE;
begin
statements;
end;
procedure SayHello(Name: String);
procedure SayHello(Name: String);
var
Greeting: String;
begin
Greeting := 'Hello, ' + Name;
WriteLn(Greeting);
end;
...
SayHello('Theodore');
Syntax
DeclarationUsage
Definition
- function funct-name: TYPE;
- function funct-name(param: TYPE; var param: TYPE): TYPE;
function funct-name(param: TYPE; var param: TYPE): TYPE;
var
arg: TYPE;
begin
statements;
funct-name := VALUE;
end;
Examples
- In Delphi, the special variable "Result" can be used to assign a return value to the function, rather than assigning a value to the function identifier.
function Add(i, j: Integer): Integer;
function Add(i, j: Integer): Integer;
var
Sum: Integer;
begin
Sum := i + j;
Add := Sum;
end;
...
var
Sum: Integer;
...
Sum := Add(1, 2);