> For the complete documentation index, see [llms.txt](https://docs.rulecube.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.rulecube.com/v2.5/how-to-guides/ruleset-development/functions.md).

# Functions

## Introduction

This guide explains how to create and use functions in rulesets.

**Functions** are a set of statements that perform a task or calculate a value and are useful to combine some functionality that you use multiple times in your ruleset. Functions are comparable to JavaScript functions, and you can create them yourself in Rulecube and use them anywhere you need their functionality.

Just one bit of info before you start creating a function. Functions need input to operate on, and an input for a function is called a **parameter**. So when calling a function, you supply its parameters, and the function returns a calculated or determined result.

## Procedure

Let's say you're developing a ruleset that determines lending risks, and you need to calculate the so-called **debt-to-income (DTI)** ratio at several places. The DTI ratio is a person's monthly debt payments divided by the gross monthly income. You can add this calculation anywhere you need, but it's much more efficient if you create a function for this and call it when needed.

1. Create a ruleset named `RiskAssessment`.
2. Create a function named `DTI`, and in the **Return type** list, select `number`.
3. Click **Add parameter**.\
   ↳ Rulecube adds a parameter row:<br>

   <figure><img src="/files/rLsWwheRkETrFkmhyRVw" alt=""><figcaption></figcaption></figure>
4. In the **Name** field, enter `monthlyDebtPayments`, and in the **Type** list, select `number`.
5. Add a second parameter named `grossMonthlyIncome`, and also set its **Type** to `number`.
6. In the **Function body**, enter the following function:<br>

   <pre class="language-javascript" data-line-numbers><code class="lang-javascript">monthlyDebtPayments / grossMonthlyIncome * 100
   </code></pre>

   \
   **Explanation**\
   In general, the DTI is calculated as a percentage as in this calculation.<br>
7. Click **Save**.\
   ↳ The DTI function is now ready to be called by its name, providing the required parameters. For example, from a rule **Expression** with `Payments` and `Income` as ruleset inputs which you supply as parameters:<br>

   <pre class="language-javascript" data-line-numbers><code class="lang-javascript">DTI(Payments, Income)
   </code></pre>

## Additional resources

* Rulecube has a list of built-in functions called methods: How-to Guide: [Use Methods](/v2.5/how-to-guides/ruleset-development/built-in-functions.md).
* You can combine your functions into a single ruleset and designate it as a library. This way, you enable your functions to be called from any ruleset (Single Point of Definition (SPOD) pattern).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.rulecube.com/v2.5/how-to-guides/ruleset-development/functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
