Functions
In KnowledgeKube, a Function is a piece of pre-defined code that instructs your application to perform one or more tasks. Instead of typing out the function's code in full every time you want to use it, you initialise it using a short expression known as a Function Call.
A call consists of the chosen function's unique keyword followed immediately by a set of round brackets (parentheses). This is known as the function's Syntax. The following example shows an expression containing a single function call:
GetDate();
Like any part of an expression, a function call must be followed by a semicolon if it appears at the end of a statement.
In the example above, GetDate is the function's keyword. Its parentheses are empty, which is a valid call for this particular function; however, some functions require additional information written between their parentheses. This information is written as a series of comma-separated values known as Arguments.
The following example demonstrates a function call with two arguments:
Chr( String, Index );
The number of arguments required is related to the number of Parameters in the function, with each function having a distinct set of parameters. The expression shown above has two parameters, the first of which takes the keyword String as its argument, while the second parameter takes the keyword Index.
An argument's value can come from either a literal or an identifier. The type of value required is determined by the function itself.
There are two types of parameter:
- Mandatory parameters must be given an argument for the function to work correctly.
- Optional parameters can be excluded from the function call if they are not needed. To exclude a parameter, either use an empty string for its argument, or omit it from the expression altogether. Either option will assign a pre-defined default argument to the excluded parameter.
In addition, some parameters are Enumerations, which require a slightly different syntax. Enumerations consist of both the name of the parameter and a value, separated by two colons:
Parameter::Value
For example, if a function contains an enumeration parameter called Colour, and you want to set this to blue, you would write the following:
ExampleFunction( Colour::Blue );
The order of a function's arguments is important. They must be written in the same left-to-right order as the function's parameters, or the function will not behave as it should. Because of this, there are two things to remember when writing a function call:
- Mandatory parameters always come before optional ones, since they cannot be excluded.
- If you omit an optional parameter - as opposed to using an empty string as its argument - you must also exclude all optional parameters that follow it. Otherwise, subsequent arguments will be used for the wrong parameter, and the function will behave incorrectly.
You can view a function's syntax, including the number of parameters it has and which of these are optional, by referring to the function's IntelliSense tool tip within the Expression Editor. The editor's specialised interface lets you edit an expression instead of typing syntax into the plain text Expression to Evaluate field.
A single function call is an expression in its own right, and as such will return a value. This means that placing a function call inside another expression will allow the larger expression to use the result of that call. Consider the following expression, which takes the higher of two numbers and subtracts from it the lower of two other numbers:
Max( 12, 9 ) - Min( 21, 4 );
Although this expression contains two function calls, it will behave exactly the same as the following expression, where each function call has been substituted with its result:
12 - 4;
Refer to the following topic to add a function to your application:
Description | Further Reading |
---|---|
Implement a function to make the process of adding expressions more efficient. | Calling a Function |