# Exploring Functions

## What is a Function? <a href="#what-is-a-function" id="what-is-a-function"></a>

Functions act similar to a [variable](https://docs.revrobotics.com/duo-control/part-2/arcade-style-teleop-blocks/establishing-variables-in-blocks#what-is-a-variable) in that we are using one thing to represent another. However, where a variable typically is used in place of something short, such as a number, a function can take the place of several lines of code.

This can be incredibly useful if there is a section of code we know will be repeated or to break apart our code into chunks for easy editing.

## Creating a new Function in Blocks <a href="#creating-a-new-function-in-blocks" id="creating-a-new-function-in-blocks"></a>

If we want to create a new function in our Blocks program, we start by pulling a ![](https://docs.revrobotics.com/~gitbook/image?url=https%3A%2F%2F268621232-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F-MHCAE012xNfg1h3SM9v%252Fuploads%252FjNRY9VTnip2Vc8wSaLGj%252Fimage.png%3Falt%3Dmedia%26token%3Dedb065d4-91c9-49f0-a3db-98a366076b28\&width=300\&dpr=4\&quality=100\&sign=8f32c37b\&sv=1)block from the Functions menu:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2Fzqkvy2cYSliszMK3JOop%2Fimage.png?alt=media&#x26;token=2e17c995-1201-4156-be73-bce0bd544c47" alt="" width="280"><figcaption><p>Functions menu in Blocks showing the functions from the 2024-25 Starter Bot</p></figcaption></figure>

Next we will replace "do something" with an appropriate name. Maybe in this case we are adding a new function for climbing:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FXsIvxm3G9vYUzEfzNw9W%2Fimage.png?alt=media&#x26;token=1db1f2d4-0785-46b8-90ea-edbcd4ef41c9" alt="" width="203"><figcaption><p>Example function for climbing</p></figcaption></figure>

Once our function is named it will appear in the "Functions" menu to be added to the main loop or wherever we need it within our code!

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FrEj4964qOIppdJKvo3Y4%2Fimage.png?alt=media&#x26;token=d481c1c6-6ec4-4984-8d55-f2173f88b221" alt="" width="278"><figcaption><p>New function block</p></figcaption></figure>

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F0ICX24xo6zU10RoIfOHe%2Fimage.png?alt=media&#x26;token=23dd4920-5ab2-4a57-86d2-dedea0eaf767" alt="" width="218"><figcaption><p>New function added to our main code</p></figcaption></figure>

All that's left is to add whatever code we'd like to be within this function:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2Fnd0orpwOvnO3LHOmTNhr%2Fimage.png?alt=media&#x26;token=ef80840c-ecce-432f-a8f1-e9903a423c3a" alt="" width="256"><figcaption><p>Example of a simple code within our function</p></figcaption></figure>

If the ![](https://docs.revrobotics.com/~gitbook/image?url=https%3A%2F%2F268621232-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F-MHCAE012xNfg1h3SM9v%252Fuploads%252Fk7zr0SUBNrnWyrt3X2EY%252Fimage.png%3Falt%3Dmedia%26token%3D3bd23c18-a128-4b5c-bbcb-4dcc179e8063\&width=300\&dpr=4\&quality=100\&sign=41933077\&sv=1) block is deleted this will remove the function from the "Functions" menu.

## When to use Functions - Example

{% hint style="info" %}
This example was originally created as part of the [2024-25 FTC Starter Bot](https://docs.revrobotics.com/ftc-kickoff-concepts/into-the-deep-2024-25/starter-bot-into-the-deep) program, but can be followed on a different robot!&#x20;

After [configuring](https://docs.revrobotics.com/duo-control/hello-robot-blocks/configuration), create a new [OpMode](https://docs.revrobotics.com/duo-control/hello-robot-blocks/part-1/test-bed-blocks) to begin. Ours is named `FunctionsDemo`.&#x20;
{% endhint %}

Let's say we are working on an autonomous code where we want our robot to drive roughly in a square. Remember that autonomous means this code will move the robot on its own when play is pressed:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2Fm9YfcT8e5RFgaeJMr88K%2Fimage.png?alt=media&#x26;token=460a026f-c3d2-4093-b736-f8592e83d5f2" alt="" width="291"><figcaption><p>Simple program for driving in a square</p></figcaption></figure>

Next, let's say we need the robot to do something between one of the turns, such as move its arm or open a servo's claw. There's a couple of ways we could approach this without functions:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2F1cjERkRY42HgfIWXe5cI%2Fimage.png?alt=media&#x26;token=091f5690-1ff7-4918-8eef-c6f9749122e3" alt="" width="164"><figcaption><p>Full code with claw opening after driving two sides</p></figcaption></figure>

Already our code is getting a little long so let's move our side motion and turn into a function:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FQBDP3JdVp6V9wCpbjSGW%2Fimage.png?alt=media&#x26;token=6a92fa4c-dc2b-4424-a842-5153ba92dd25" alt="" width="235"><figcaption><p>Function containing the code for driving and turning</p></figcaption></figure>

Now our loop may look like this:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2FWkvEhTMEbuItbgGJRjuL%2Fimage.png?alt=media&#x26;token=aa76b595-bb82-4f4d-930e-9b8ab51010e6" alt="" width="417"><figcaption><p>Using functions to drive in a square</p></figcaption></figure>

When we test our code we may notice our robot isn't exactly driving in a square shape. Thankfully with our function in place we only need to change the needed value in one place:

<figure><img src="https://1359443677-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FUOOiQ4S2QcMWmVoSmeQ8%2Fuploads%2Fe2mjJNtqLqqQAiMBqetD%2Fimage.png?alt=media&#x26;token=795543e0-cc32-495b-a208-59d08a6e6f2c" alt="" width="235"><figcaption><p>Code for controlling the robot's turn</p></figcaption></figure>

This change to the function will be reflected anywhere `DRIVE_AND_TURN` used.

Give it a try by changing the right motor's power or the timer to refine your square!


---

# 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.revrobotics.com/duo-control/hello-robot-blocks/part-4-going-beyond/exploring-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.
