# Functions

## What is a Function?

The term *function* is used to describe a set of reusable and self-contained "block of blocks" that accomplish a specific task.

A *function* is a reusable, self-contained "block of blocks" designed to accomplish a specific task. They work great for blocks that you want to reuse many times over.

## The Function Blocks

There are two kinds of blocks that you can use:

1. to
2. to return

### Function: To

The **to** function in the Dynamic Engine is a fundamental programming block used to group a set of code instructions or tasks into a reusable and organized function. Unlike the **to return** function, which returns a value after execution, the **to** function performs operations only and does not return a specific result. It is primarily used for its side effects, such as modifying variables or creating outputs within the program. The code encapsulated within a **to** function can be called and executed multiple times throughout the program, enhancing code reusability and efficiency.

You first have to **set** the function, and then you'll **get** the function to call it.

For example, let's say you like to randomize names in most of your questions. Sometimes you want to use a first name, sometimes a last name, and sometimes a full name. Instead of building this for every question, you can simply create a to function. Here is an example:

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FO6rDW1SguLmKTNpN1zUn%2FScreenshot%202024-12-06%20at%203.45.39%E2%80%AFPM.png?alt=media&#x26;token=6d4b3b69-d6f3-4700-af39-d23b4fafa869" alt=""><figcaption></figcaption></figure>

Anything that you can do inside of the Dynamic Engine, you can put inside of a function. To call the function, all you have to do is use the **get** component of the function block:

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FxXlule6uZJh2gkDzr5hG%2FScreenshot%202024-12-06%20at%203.53.14%E2%80%AFPM.png?alt=media&#x26;token=b3d2d446-63e2-45fc-8962-c682eb08a62b" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Put your functions inside of [The Backpack](https://examind.gitbook.io/v1/build/question-builder/dynamic-questions/dynamic-engine/the-backpack) for easy retrieval across questions.
{% endhint %}

### Function: To Return

The **To Return** function in the Dynamic Engine allows you to create reusable blocks of code that perform specific tasks and return a value. Utilizing the **to** block, you define the function's logic, specify any necessary inputs, and determine the output value that is returned when the function is called. This enables efficient code management and simplifies complex operations by encapsulating functionality within named functions, making it easier to implement consistent logic across different parts of your project.

Another way to think about the **To Return** function is a custom formula for a specific variable. For example, if we are always calculating straight-line depreciation, it is easier to have a function we can call up and just send the values.

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FvyZZkuKIi5HOVRvEdPg0%2FScreenshot%202024-12-06%20at%204.03.56%E2%80%AFPM.png?alt=media&#x26;token=2f4a491d-cb51-43ab-88a1-979cd853dbf1" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
To keep things clean, right click a function and select "collapse block". This will give you more space to work.
{% endhint %}

To **get** the calculation to work, we simple attached the function to a variable and its values:

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FIcurhIJRoUZVCprWCgjM%2FScreenshot%202024-12-06%20at%204.04.46%E2%80%AFPM.png?alt=media&#x26;token=eb885e6a-c414-48bb-a00f-5cbc5917f3af" alt=""><figcaption></figcaption></figure>

***

## Example Question

Consider the following Fill-in-the-blank question in EXAMIND:

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2F5yVCKYdlnLnstcr8bNUb%2FScreenshot%202024-12-06%20at%204.17.36%E2%80%AFPM.png?alt=media&#x26;token=a449ca7b-80d7-4fdc-876b-a6cbf6c8eeae" alt=""><figcaption></figcaption></figure>

Here is what the Dynamic Engine would look like:

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FBsgWSWEBDRB52YnuYvRG%2FScreenshot%202024-12-06%20at%204.17.52%E2%80%AFPM.png?alt=media&#x26;token=26cb5972-5538-4bdc-8335-821e9b96f3d1" alt=""><figcaption></figcaption></figure>

The **Random Name** and **Asset Purchase Data** functions would look like this:

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FVULFPjQ9zLDgScrJChwt%2FScreenshot%202024-12-06%20at%204.20.22%E2%80%AFPM.png?alt=media&#x26;token=9b95e7d9-05ac-4784-8e7a-c95e68895161" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
Both the **set** and **get** part of a function needs to be in the Dynamic Engine to work properly
{% endhint %}

Here is what a sample question would look like (values highlighted in blue for convenience):

<figure><img src="https://211323355-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FDfKsc0R4AcYm9kZgIT2X%2Fuploads%2FqMP4P3ChCGabFlYt44UK%2FScreenshot%202024-12-06%20at%204.17.26%E2%80%AFPM.png?alt=media&#x26;token=088f2231-75b3-45ab-9fa0-5bc3c7d939e3" alt=""><figcaption></figcaption></figure>
