# Creating Custom Functions

This guide walks through the process of creating two new [User Defined Functions](https://github.com/airkit/airkit-docs/blob/master/docs/v18-4/user-defined-functions.md). The first example takes a [Number](../../variable-data-types/the-number-variable-data-type) as input, cubes it, and returns the absolute value of the cubed result. The second example takes a [Number](../../variable-data-types/the-number-variable-data-type) as input and uses recursion to compute its factorial.

## A UDF without recursion

UDFs are created from [Connections Builder](../../studio/studio/builder-bar/connections-builder) by clicking on the '+' icon next to **User Defined Functions** in the Tree:

![](https://2599974655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIRF6etABDVMr59RdpyGw%2Fuploads%2Fgit-blob-ce8df60b69c554f372a30d4e979a6318d7a1e2cd%2Fcreating-custom-functions-v184-0.png?alt=media)

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

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

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

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

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

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

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

<figure><img src="https://2599974655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIRF6etABDVMr59RdpyGw%2Fuploads%2Fgit-blob-4e07e40be9d30f3affab4b77142e6e76ff484e06%2Fcreating-custom-functions-v184-4.png?alt=media" alt="" width="100"><figcaption></figcaption></figure>

In the **Function Body** that appears in the Stage, use [Airscript](../../working-with-data/working-with-data-overview/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://2599974655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIRF6etABDVMr59RdpyGw%2Fuploads%2Fgit-blob-0fc0db86baaea99b9d708c93e4980806ec54824e%2Fcreating-custom-functions-v184-5.png?alt=media)

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](../../working-with-data/working-with-data-overview/airscript-quick-start) 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](../../airscript-expressions/math-functions/fact) function.

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

<figure><img src="https://2599974655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIRF6etABDVMr59RdpyGw%2Fuploads%2Fgit-blob-25807dcab2b06a9765680ae31235d8fa0fa5cdce%2Fcreating-custom-functions-v184-6.png?alt=media" alt="" width="400"><figcaption></figcaption></figure>

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.

<figure><img src="https://2599974655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIRF6etABDVMr59RdpyGw%2Fuploads%2Fgit-blob-38df1c81a2c23418d8c64b5adc91c385abb734b4%2Fcreating-custom-functions-v184-7.png?alt=media" alt="" width="400"><figcaption></figcaption></figure>

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))
```

![](https://2599974655-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIRF6etABDVMr59RdpyGw%2Fuploads%2Fgit-blob-7b2fd2022e0212b7efc0d08b4bcc17b44a33ab6c%2Fcreating-custom-functions-v184-8.png?alt=media)

Upon saving, this custom function will be accessible in other parts of the [Studio](../../studio/studio), to be called on like any other [Airscript](../../working-with-data/working-with-data-overview/airscript-quick-start) function.
