User-defined Functions
User-defined functions are functions that are built and declared by the user in a particular script.
They are classed as local functions by default, which means that they are only available and accessible in the script in which they are declared.
However, it is possible to make a user-defined function available in multiple locations, by creating and declaring the function in an include file. Please review the information on Include Files.
You may find that it can be very educational in learning Statelake (as well as fun) to write your own functions.
Aside from the User-defined Functions that are illustrated in Defining Or Declaring A Function , these may also be of interest.
Add
Here’s a simple one to get you started. Instead of using the plus symbol (+), you could use this Add function.
function
Add(Num1, Num2 :
integer
) :
integer
;
begin
result := Num1 + Num2
end
;
procedure
ScriptEvent(
var
Value : variant);
var
aNumber, bNumber :
extended
;
begin
aNumber := Add(
57
,
100
);
//Result is 157
LogInfo(FloatToStr(aNumber));
bNumber := Add(
57864
,
432259
);
//Result is 490123
LogInfo(FloatToStr(bNumber));
end
;
Hello
A nice easy function to say hello.
function
Hello(aName :
string
) :
string
;
//Result will be a STRING data type
begin
//Assign the result value to the system variable named RESULT
Result := ‘Hello ‘ + aName;
end
;
procedure
ScriptEvent(
var
Value:Variant);
begin
//Use Hello() function to set Value to ‘Hello World’ ...
Value := Hello(‘World’);
end
;
Reverse
A little more in-depth, this function does the same thing as the pre-defined function ReverseString.
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
//Using REVERSE function
WhatItIs := Reverse(
'Tommy'
);
//The result is 'ymmoT'
LogInfo(WhatItIs);
end
;