Defining a Script Function
You can use the scripting language to define functions that can be reused in other places. Typically, functions take parameters that define the data on which the function operates, and return a value which indicates their result.
Defining a Function
function ShowMessage(%name, %value)
{
%msg = strcat("The value of ", %name, " is ", %value)
%reply = MessageBox(%msg)
return %reply
}
%res = ShowMessage("Test", 42)
This example shows all elements of a script-defined function:
The
function
keyword introduces the function definition. It is followed by the name of the function, and then, by the list of parameters which the function takes. The parameter names should be chosen so that they can be connected with their meaning. They are scoped to the function, i.e. the names are only known inside the function.The curly braces
{...}
enclose the function body, i.e. the sequence of instructions that are executed when the function is called.Local variables, introduced by the per cent sign (
%
), are scoped to the function.Parameters are referenced like local variables.
The
return
statement ends function execution and transports a result value to the caller. Note that, unlike in most programming languages, a function must always return a value, although the caller may ignore it.To call a function, use its name, followed by the list of actual arguments. The number of arguments must match the number of parameters in the function definition.
Usage of parentheses (...)
around the parameter list and around the list of actual arguments is mandatory.
The function body can contain any number of return
statements (with specific result values) which cause premature exit. However, at least one return
is required because execution must not reach the final closing brace.