Getting started with functions
Functions are a special type of formulas. Basically, they are pre-made formulas that do a specific thing. For example, if you want to know the age of someone, you don’t need to figure out yourself how to calculate that. You can just use our
Functions are always structured in the same way: the name of the function followed by two parentheses. Within the parentheses, you give some information to the function (like someone’s birth date and the current date).
The following example shows how a function can be used to calculate the age of the user. In a new decision tree model, add a new question to the node start. Select the Date input type and give your question an appropriate name, such as ‘birthday’.
In the same node ‘start’, add a formula. Formulas can be found under the Actions menu. Give the formula the name ‘age’.
In the bottom left of the formula screen there is a button with a magnifying glass. If you press it, a new screen will pop up in which you can find formulas. Make sure to select the formula field.
In the panel that opens, you see a variety of different functions grouped by subject on the left. The function we are looking for, is called
age() and can be found under the ‘Date’ subject. If you doubleclick
age(), you can begin to add the required information for the function.
We call this information parameters. Some functions need two parameters, like
age(), but others may require more – or none at all. Below, you see the selection window that helps you get the right information.
On the left you will see ‘age(’ in the formula field. The open bracket means you will have to enter the parameters. It also mentions which parameters, right below the formula field. In the previous image, you could see examples of how the age function works. To calculate someone’s age, we need both the current data and his or her date of birth.
To do so, go to the Functions tab and doubleclick
now under Date.
now() is a function that gives you the current date and requires no parameters. The Studio will now switch to the Data tab, where you can select the date of birth question.
Important Make sure to separate all parameters with a comma, and end the last parameter with a ‘)’!
The resulting function should now look like this in your Actions panel:
You are now able to show the result of the formula in a node by following the same steps as in the previous tutorial. Use a text quoting the formula variable. Don’t forget the caron (
^) before the formula name!
There are many different functions available that increase your decision tree modelling efficiency. You can always look them up and see what they do: a help section is provided in the screen where you can choose which function to add. If you know how a function works and know which variables it takes, you can also type in the formula in the Formula field.