Important note: all examples follow aTbRef naming conventions.
Defining functions
Functions are defined by the function statement:
function name(arguments){actions}
The basic syntax is comprises four parts:
- the function action code. This tells Tinderbox that a function is about to be defined.
- the function's name. For function naming rules/constraints, see naming functions. The function name must be separated from the function code by a single space.
- parentheses () containing the function's input arguments, i.e. (arguments). The parentheses must be used even if there are no arguments—that is, use (). See more on setting arguments. Also:
- There must be no white space before the parentheses. White space between the parentheses and the opening { of the action code is allowed, but is deprecated so as to give an easier rule to follow: no white space before or after the obligatory parentheses.
- A value must be passed for every argument defined in the function. To simulate an optional argument, pass a null value (empty string
""
, zero0
,false
, etc.) and test that input within the function's code. - The need for parentheses when there are no arguments is a slight exception to action code operator syntax where Tinderbox is generally happy for parentheses to be omitted if there are no arguments being required/used. Thus, functions are an exception to that principle.
- curly braces {actions} enclosing the function's complete action code, i.e. one or more discrete code expressions. These curly braces must be used. There is no space between the preceding parentheses and the curly braces. Though Tinderbox appears to cope with white space in these positions, such use is not recommended: assume no white space is allowed here. Unless other code follows the function code, there should be no need to place a semi-colon after the closing curly brace.
A function is essentially an encapsulated set of action(s) which may take inputs—arguments—similar to the way some action code operators take input arguments. The terms 'argument', 'input' and 'argument' may be used interchangeably, as the implied meaning is the same.
Storage of function code is discussed here.
A function can be called from anywhere, as explained here.
Examples of function syntax are given here.
Can a function code contain another complete function?
Next: Function arguments