User defined functions - Basic Syntax
The content of a user function written in Basic Syntax is defined as follows
function name( [parameter list] ) [as type] //statements// end function
Like any other basic formula, the user function has to assign its return value to a variable. But unlike normal formula fields, this variable has the name of the function itself instead of 'formula'. If the return value of the function is defined by the function header (as type), the return value assignments will be checked against this type. Please note that neither range nor array can be used here.
Parameter list
A function can have zero or multiple parameters (which are then comma-separated). The parameter definition is similar to defining a variable without scope. For a function, any parameter is a variable with scope local. A parameter has to be defined as follows
[optional] [range] name[()] as type [= //default//]
term | optional | description |
---|---|---|
optional | X | Indicates that this parameter can be skipped when calling this function. If a parameter is optional, every following parameter has to be optional as well. Furthermore you have to define a default value for any optional parameter. |
range | X | marks this parameter as ranged type |
name | - | the name of the parameter, duplicates are not allowed |
() | X | marks this parameter as an array type, when combined with range, the type is an array of ranged values |
as type | - | the value type of this parameter |
= default | X | the default value, which is required if this parameter is marked as optional. The default is a normal Basic Syntax variable declaration but it must have a constant value. This value will be used, if the parameter was left out when calling the function. |
Exit statement
Unlike a Crystal syntax function, a Basic syntax function can exit at any time. To quit a function. insert exit function at the desired position. The return value of the function will be the value of the return value variable. If no value has been assigned, null is returned. This may cause the calling formula to interrupt depending on the 'Null behavior' setting.
Example
The following example shows a simple add / subtract function for numbers. An optional third parameter switches between add and subtract with add as default. We name this function 'addsubtract'.
function addsubtract ( x as number, y as number, optional add as boolean = true ) as number if add then addsubtract = x + y else addsubtract = x - y end if end function
These are the results when calling this function
addsubtract( 2, 1 ) ==> 3 addsubtract( 2, 1, true ) ==> 3 addsubtract( 2, 1, false ) ==> 1