Creating Custom Functions
Creating Custom Functions: "User Defined Functions"
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
- In the Connections 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.
- Under the Usageย section in the Inspector, define the Namespace value as "ABS_CUBED" and leave the Binding value as "USER_FUNCTION".
- 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":
- 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:
Select Number from the dropdown menu on the left and label this Number variable "n":
- Directly under the Outputfieldย 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:
- 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:
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
- Toggle over to the Activity Builderย and insert a Label into a Web Page:
- 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:
- 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:
Updated over 1 year ago