Creating a new User Defined Function Creating a new User Defined Function

Creating a new User Defined Function

Chandra Bergmann Chandra Bergmann

This guide walks through the process of creating a new User Defined Function – specifically, one that takes a Number as input, cubes it, and returns the absolute value of the cubed result. 

Creating a User Defined Function

1. In the Connection Builder, add a new function by clicking on the '+' icon next to User Defined Functions in the Tree and selecting User Function Definition from the menu that appears.

Screen_Shot_2021-11-02_at_10.03.30_AM.png

2. 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

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

Screen_Shot_2021-11-02_at_10.17.05_AM.png

4. Under the Behavior section in the Inspector, define the expected input. ABS_CUBED#USER_FUNCTION will require only a single input: a Number. Click on the '+' icon under Inputs:

Screen_Shot_2021-11-02_at_10.17.39_AM.png

Select Number from the dropdown menu on the left and label this Number variable "n":

Screen_Shot_2021-11-02_at_10.18.01_AM.png

5. Directly under the Output field in the Behavior section in the Inspector, define the expected output. ABS_CUBED#USER_FUNCTION will output Number, so select "Number" from the dropdown menu under Output:

Screen_Shot_2021-11-02_at_10.18.43_AM.png

6. 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.

Testing a User Defined Function to ensure it works as intended

1. Toggle over to the Activity Builder and insert a Label into a Web Page:

Screen_Shot_2021-11-02_at_10.20.29_AM.png

2. In the Inspector, access Control Properties -> Text and define a label with Airscript that utilizes ABS_CUBED#USER_FUNCTION. Hardcode the input with the expected output in mind. In this case, give ABS_CUBED#USER_FUNCTION the Number -3 (the absolute value of the cube of which is 27).

Label Web Controls expect to be given Text to display on the Web Page. When testing a User Defined Function that returns something other than Text, this will need to be accounted for. Recall that ABS_CUBED#USER_FUNCTION returns a Number; to covert this Number into Text, use it as input for the FORMAT_NUMBER function:

FORMAT_NUMBER(
ABS_CUBED#USER_FUNCTION(
-3
)
","
)

Inserted in the Text field under Control Properties, this will appear as follows:

Screen_Shot_2021-11-02_at_10.26.04_AM.png

3. Once the Airscript has been finalized, check to see if the output appears as expected in the Stage. The absolute value of the cube of -3 is 27, and this is what appears in the Label, indicating that the User Defined Function works as expected:

Screen_Shot_2021-11-02_at_10.26.18_AM.png

 

Was this article helpful?

0 out of 0 found this helpful