# Creating Custom Functions

This guide walks through the process of creating a new [User Defined Function](https://github.com/airkit/airkit-docs/blob/master/docs/v17-15/user-defined-functions.md) – specifically, one that takes a [Number](../../variable-data-types/the-number-variable-data-type) as input, cubes it, and returns the absolute value of the cubed result.&#x20;

#### 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](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-ce8df60b69c554f372a30d4e979a6318d7a1e2cd%2Fcreating-custom-functions-v1715-0.png?alt=media)

2. Under the **Usage** section in the Inspector, define the Namespace value as "ABS\_CUBED" and leave the Binding value as "USER\_FUNCTION".

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-731cdd2553e31b1a64263b5c4b26964eb8fc3ec2%2Fcreating-custom-functions-v1715-1.png?alt=media)

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":

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-abc55adf142f1fc2090b1db8e6cb8b668202d6fc%2Fcreating-custom-functions-v1715-2.png?alt=media)

4. Under the **Behavior** section in the Inspector, define the expected input. ABS\_CUBED#USER\_FUNCTION will require only a single input: a [Number](../../variable-data-types/the-number-variable-data-type). Click on the '+' icon under _Inputs_:

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-31e5e1978ce97882c934d31eca6bf96f66fe1335%2Fcreating-custom-functions-v1715-3.png?alt=media)

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

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-8eae7f3fff5c1ddc116918764e87ab6e5a5757c4%2Fcreating-custom-functions-v1715-4.png?alt=media)

5. Directly under the **Output**field in the **Behavior** section in the Inspector, define the expected output. ABS\_CUBED#USER\_FUNCTION will output [Number](../../variable-data-types/the-number-variable-data-type), so select "Number" from the dropdown menu under _Output_:

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-4e07e40be9d30f3affab4b77142e6e76ff484e06%2Fcreating-custom-functions-v1715-5.png?alt=media)

6. In the **Function Body** that appears in the Stage, use [Airscript](../../airscript/airscript-quick-start) to define the output of ABS\_CUBED#USER\_FUNCTION in terms of the given input. The following code utilizes the Airscript function [ABS](../../airscript-expressions/math-functions/abs) as well as the multiplication operator in order to return the absolute value of the cube of the number **n**:

```javascript
ABS(  
 n * n * n  
)
```

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

![](https://a01-support.airkit.com/creating-a-new-user-defined-function/Screen%20Shot%202021-11-02%20at%2010.19.26%20AM.png)

We have now created a function, ABS\_CUBED#USER\_FUNCTION, that takes a [Number](../../variable-data-types/the-number-variable-data-type) 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](../../studio/studio), to be called on like any other [Airscript](../../airscript/airscript-quick-start) function.

#### Testing a User Defined Function to ensure it works as intended

1. Toggle over to the [Activity Builder](../../studio/builder-bar/web-builder) and insert a [Label](../../web-controls/label-web-control) into a [Web Page](../../studio/web-flows):

![](https://a01-support.airkit.com/creating-a-new-user-defined-function/Screen%20Shot%202021-11-02%20at%2010.20.29%20AM.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](../../variable-data-types/the-number-variable-data-type) -3 (the absolute value of the cube of which is 27).

[Label Web Controls](../../web-controls/label-web-control) expect to be given [Text](../../variable-data-types/the-text-variable-data-type) 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](../../variable-data-types/the-number-variable-data-type); to covert this Number into Text, use it as input for the [FORMAT\_NUMBER](../../airscript-expressions/number-formatting-functions/format_number) function:

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

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

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-7bbd4272508ab49d216dd8f7ccf00146de74fa37%2Fcreating-custom-functions-v1715-6.png?alt=media)

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:

![](https://1191388742-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FhOn9CLWoV9VfJeM5EDAa%2Fuploads%2Fgit-blob-5bf1337b5423c6183ab3ddc5727f23b9b361d64a%2Fcreating-custom-functions-v1715-7.png?alt=media)
