# Functions Blocks

## Overview

A function is a block of code that performs a specific task. If you use the same code set multiple times in your project, you can work more efficiently using functions. Clear function names also make your code more readable.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FKPN4ZhisupkzNLVYZc4a%2Ffunctions_overview.png?alt=media&#x26;token=a018a06e-f0fe-42a2-9a22-3a638d475c22" alt=""><figcaption></figcaption></figure>

## Video Tutorial

{% embed url="<https://www.youtube.com/watch?v=3jS3F3IRQB0>" %}

## Create a Function

Use the following block to create a function and give it a name.

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FLtrjqipsrpxRxyu53une%2Fplus_function_block.png?alt=media&#x26;token=7f615fa7-ac63-4d34-a26a-e2e313c8f55c" alt=""><figcaption></figcaption></figure></div>

One example of a function is below. This function will create and shuffle a list of items.

<figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2Fu9pPnkPbPKHVxmpPllST%2Ffunction_shuffle_example.png?alt=media&#x26;token=2f99d543-83d5-48f6-8f1b-d1b2393cbd38" alt=""><figcaption></figcaption></figure>

Once you have created your function, you can use it at any time using the function block you have named.

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FSB4wQJb4PKuXu4AiVVJT%2Ffunction_call.png?alt=media&#x26;token=420a9f86-5d72-4f80-bca4-c5a6d0b42581" alt=""><figcaption></figcaption></figure></div>

## Create a Function with an Output

You can also create a function that returns an output.

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2F9GRfZGBXAnpUlG7ho530%2Fplus_do_something_and_return_block.png?alt=media&#x26;token=40078312-0c1c-4c14-9fbc-ce1472df9b05" alt=""><figcaption></figcaption></figure></div>

### Conditional Outputs

Adding multiple "return" blocks to your function allows you to provide different outputs depending on specific conditions. This means your function can evaluate various scenarios and return distinct results based on your predefined conditions.

For example, in the block combination below, the function returns a description of the quality of the item being reviewed according to the ratings provided.

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FPrOryldJQsCxopFuHuFd%2Fconditional_outputs.png?alt=media&#x26;token=cb904fe7-44a3-4553-8832-86abbd94fa68" alt=""><figcaption></figcaption></figure></div>

The function stops when the "return" block fires. For example, in the above example, if the rating is 100, the function will return **Perfect** and then stop. It will not run the other tests and won't return **Excellent**. **Good**, **Fine**, etc.

## Inputs and Functions

Click the plus icon to add input variables to your function.&#x20;

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2FdBiHZ6PCFEVhJPRmbY2k%2Finputs_and_functions.png?alt=media&#x26;token=b03110c5-fa98-4963-83f9-1ca48bdb3d30" alt=""><figcaption></figcaption></figure></div>

You can then treat that input as a local variable in your function.

In this example, the function "change font size" takes a number as an input and sets the screen's text to this font size. The number can be fed in as the number selected on a [slider](https://docs.thunkable.com/app-design/ui-components/input-components/slider), or the user can click a reset [button](https://docs.thunkable.com/app-design/ui-components/basic-components/button) to set the font size back to 18.

<div align="left"><figure><img src="https://1223006790-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LAn5scXl2uqUJUOqkJo-84897653%2Fuploads%2Fk6eQWMgBCVKBtaRiMG8p%2Finputs_in_functions.png?alt=media&#x26;token=fe58e717-3f00-486f-acc8-569da3bd96a5" alt=""><figcaption></figcaption></figure></div>


---

# Agent Instructions: 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.thunkable.com/blocks/blocks/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.
