Creating Custom Functions

Creating Custom Functions: "User Defined Functions"

This guide walks through the process of creating two new User Defined Functions. The first example takes a Numberย as input, cubes it, and returns the absolute value of the cubed result. The second example takes a Numberย as input and uses recursion to compute its factorial.

A UDF without recursion

UDFs are created from Connections Builder by clicking on the '+' icon next to User Defined Functions in the Tree:

Screen_Shot_2021-11-02_at_10.03.30_AM.png

For this example, under the Usageย section in the Inspector, define the Namespace value as "ABS_CUBED" and leave the Binding value as "USER_FUNCTION".

Screen_Shot_2021-11-02_at_10.05.24_AM.png

Then rename the function in the Tree so that it corresponds to how the function will be called upon within the application. User Defined Functions are designated asย Namespace#Binding, so this function will be renamed "ABS_CUBED#USER_FUNCTION":

Screen_Shot_2021-11-02_at_10.17.05_AM.png

Under the Behaviorย section in the Inspector, define the expected input. ABS_CUBEB#USER_FUNCTION will require only a Number as a single input. Click in the '+' icon under Inputs and select Number from the dropdown menu. Label this Number variable "n":

Screen_Shot_2021-11-02_at_10.18.01_AM.png

Since ABS_CUBED#USER_FUNCTION will output aย Number, select "Number" from the dropdown menu underย Output:

Screen_Shot_2021-11-02_at_10.18.43_AM.png

In the Function Body that appears in the Stage, use Airscript to define the output of ABS_CUBED#USER_FUNCTION in terms of the given input.ย The following code utilizes the Airscript function ABS as well as the multiplication operator in order to return the absolute value of the cube of the number n:

ABS(  
 n * n * n  
)

Inserted in the Function Body, this will appear as follows:

Screen_Shot_2021-11-02_at_10.19.26_AM.png

We have now created a function, ABS_CUBED#USER_FUNCTION, that takes a Numberย as input, cubes it, and returns the absolute value of the cubed result. Upon saving, this custom function will be accessible in other parts of the Studio, to be called on like any other Airscript function.

A UDF using recursion

UDFs allow recursion, meaning that a UDF can call on itself recursively. To present a simple case, weโ€™ll create a UDF that mimics the FACT function.

Weโ€™ll start by creating a new UDF in Connections Builder and changing the Namespace and Binding under Usage as follows:

The function will be automatically renamed to "MATH#FACTORIAL".

This UDF will also receive and retrieve numbers. Therefore, in the Behavior section, add a Variable of type Number and call it num. Then select type Number as the Output.

In Function Body, weโ€™ll use the following Airscript function to get the factorial of the Variable "num".

IF(num = 0 OR num = 1, 1, num * MATH#FACTORIAL(num - 1))

Upon saving, this custom function will be accessible in other parts of the Studio, to be called on like any other Airscript function.