User defined functions
TPL 1.5 introduces user defined functions. These can be imported from other modules and reused.
User defined functions are specified in
definitions blocks. In order to specify a function the
type parameter within the
definitions block must be set to the value
function or omitted.
define within the definitions block specifies a function. A function definition consists of:
- A pair of parentheses containing an optional list of identifiers. The identifiers represent the number of parameters specified in a call to the function and the identifier used to reference each parameter
- An optional return list specified by following the close parenthesis with '->' and a list of identifiers, one for each return value.
- A description string
- A list of statements
The following example shows a simple example function:
You can return a comma separated list of values using the
return statement. This list must have the same number of values as specified by the function definition. The return value identifiers are purely symbolic and not referenced anywhere within the function call or definition. For example:
You can specify a default value for function parameters. However, a default value must be a simple value, such as a number or a string. You cannot use either a table or a list as a default value. Any parameter following the first that has a default value must also have a default value. In the following example, calling
add with a single parameter
Here are some more simple examples of functions.
The following pattern contains calls to functions defined in the same TPL module. The functions are the examples given in the preceding section.
Recursive and mutually recursive functions are not supported. Attempts to use recursion will be diagnosed, preventing the pattern module from being activated.
Functions calling functions
If you define a function which calls another function, the function being called must be defined before the function doing the calling, otherwise the pattern will not activate.