Function Parameters
A Function is a sub-routine that returns a value, by using Parameter values (Arguments) to produce a result which is returned in a named variable.
function
SmallLetters(
const
aString :
string
):
string
;
begin
result := LowerCase(aString);
end
;
If a Function accepts multiple parameters they should be separated with commas such as in the following example.
Value := FunctionName(param1,param2,param3,. . . .);
Another example would be as follows, where a value is passed in for the Parameter called UpperLev and a value is passed in for the Parameter called LowerLev.
function
PowerLevel(UpperLev :
integer
; LowerLev :
integer
):
integer
;
You must always pass in a value for each parameter listed within the parentheses - if three parameters are required then you must pass in all three parameters. It is not possible to completely skip or ignore a parameter, however you can pass a blank or null value in some cases.
A Function will always return a single resulting value, however, you can use the var and out keywords to allow the Parameter to be treated as if it were a variable.
However, a Function can also have no Parameters.
function
WF:
Integer
;
begin
Result :=
17
;
//Hard-coded value
end
;
procedure
ScriptEvent (
var
Value : variant);
begin
LogInfo(IntToStr(WF));
//WF will always return 17
end
;
The value from a Function is returned in a special variable called Result that is automatically defined to be the same type as the return type of the Function. This special variable can be assigned at any point in the Function - and when the Function is complete, the value is held in this special variable and is available to the program. Using Result in the Function, this sample code reverses the string entered - so Tommy becomes ymmoT.
function
Reverse(
const
S:
string
):
string
;
//Function reverses the string
var
I, J:
Integer
;
begin
I :=
1
;
J := Length(S);
SetLength(Result, J);
while
I <= J
do
begin
Result[I] := S[J];
Result[J] := S[I];
Inc(I);
Dec(J);
end
;
end
;
procedure
ScriptEvent (
var
Value : variant);
var
WhatItIs :
string
;
begin
WhatItIs := Reverse(
'Tommy'
);
LogInfo(WhatItIs);
end
;
This simpler Function declaration, uses Num1 and Num2 as Parameters.
function
AddIt(Num1, Num2 :
integer
) :
integer
;
begin
result := Num1 + Num2
end
;
procedure
ScriptEvent (
var
Value : variant);
var
iNumber :
integer
;
begin
iNumber := AddIt(
10
,
20
);
LogInfo(IntToStr(iNumber));
end
;