Inno Setup Preprocessor: User Defined Functions

In addition to the built-in support functions, you can define your own functions.

A user defined function declaration consists of a formal parameter list and an expression. That expression is evaluated when the function is called (see below). The result of the function call is the result of the expression. The expression can contain parameter names, they are treated as usual variables.

The formal syntax of a user defined function is shown in define and ISPPBuiltins.iss contains many example functions.

Note that there must be no space between the function name and the opening parenthesis.

The formal parameter list can be empty, in which case the function takes no parameters. Without parentheses, a variable is defined instead of a user defined function.

Actual parameters for parameters declared as by-reference (*) must be modifiable l-values (in other words, defined variables or expressions that evaluate to l-values). If the expression modifies a by-reference parameter, the variable passed as this parameter is modified. By-value parameters can also be modified by the expression (using assignment operators), but this doesn't affect the value of a variable passed as this parameter.

Though a user defined function can only contain one expression, sequential evaluation operators (comma), assignment operators (simple and compound), and conditional operators (?:) can be used to build more complicated functions.

Function and array parameters

A parameter declared with the func type-id accepts a user defined function or built-in function. A parameter declared with the array type-id accepts an array variable. When calling a user defined function that has such a parameter, the actual parameter must be specified using the @ operator followed by the name of the identifier to pass. For example:

#define Apply(func Callback, str Value) Callback(Value)
#define Decorate(str S) "(" + S + ")"
#emit Apply(@Decorate, "hello") ; emits "(hello)"

And:

#dim MyData[3] {10, 20, 30}
#define Sum3(array A) A[0] + A[1] + A[2]
#emit Sum3(@MyData) ; emits 60
#define GetDim(array A) DimOf(A)
#emit GetDim(@MyData) ; emits 3

Local array

In the context of the expression, an additional array named Local is valid. Its elements can be used for temporary storage and reusing values in sequential expressions. Values stored in the Local array are neither preserved from call to call (including recursive), nor are they accessible from anywhere except the expression.

#define DeleteToFirstPeriod(str *S) /* S is by-reference */ \
  Local[1] = Copy(S, 1, (Local[0] = Pos(".", S)) - 1), \
  S = Copy(S, Local[0] + 1), \
  Local[1]

See also

Extended User Defined Function Call Syntax