# Programming - Creating Functions

This year in our Starter Bot Blocks code we are using "functions" for the first time.&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FmQQfWRfiFHc0xN0gEHiU%2Fimage.png?alt=media&#x26;token=5b88d408-0f9d-43bd-b5be-65f8a39456ef" alt=""><figcaption><p>Blocks Functions</p></figcaption></figure>

## What is a Function?

Functions act similar to 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 or equation, a function can take the place of several lines of code.&#x20;

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.

Below is a breakdown of our functions:

<table><thead><tr><th width="292">Function</th><th>Purpose</th></tr></thead><tbody><tr><td>GAMEPAD_INPUT_STATE</td><td>Contains the code related to the arm/wrist presets</td></tr><tr><td>GAMEPAD_INPUT_TOGGLE</td><td>Contains the code for the claw toggle</td></tr><tr><td>GAMEPAD_INPUT_MANUAL</td><td>Contains the code for manual control of the arm/wrist</td></tr><tr><td>GAMEPAD_INTAKE</td><td>Contains the code for running the servo on the intake</td></tr><tr><td>STATE_MACHINE</td><td>Contains all the preset positions for currentState</td></tr><tr><td>SPLIT_STICK_ARCADE_DRIVE</td><td>Contains the code for driving the robot</td></tr><tr><td>TELEMETRY</td><td>Contains the telemetry read out code</td></tr></tbody></table>

### How do Functions appear in Blocks?

When using functions, our Blocks program becomes divided into different sections containing separate series of code:

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FMQ3O9gvV0y1ujBSA1xRG%2Fimage.png?alt=media&#x26;token=1f14fa2c-04c6-4f27-9ab7-99c02fb131dc" alt=""><figcaption><p>Starter Bot 2024-25 Example Code </p></figcaption></figure>

So let's say we wanted to change how quickly our robot's arm moves during [manual control](https://docs.revrobotics.com/ftc-kickoff-concepts/into-the-deep-2024-25/programming-controlling-the-arm-and-wrist#manual-control) with the d-pad. With our functions organizing our code in chunks, it's easy to find the values we want to change:

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FcO5SMhhTMTvaa6JT8ah6%2Fimage.png?alt=media&#x26;token=86ebceee-2d9e-4788-8c39-b29b0c9d989b" alt=""><figcaption><p>Function for Manual Arm/Wrist control</p></figcaption></figure>

Without the use of functions our code would end up a little long and clunky:

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FdMSYt3XC8fqeZagdGJa2%2Fimage.png?alt=media&#x26;token=3be439bf-1c19-45b7-be1f-8f9aa7563285" alt=""><figcaption><p>This is intended as an example only and not actual use!</p></figcaption></figure>

### Creating a new Function in Blocks

If we want to create a new function in our Blocks program, we start by pulling a ![](https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FjNRY9VTnip2Vc8wSaLGj%2Fimage.png?alt=media\&token=edb065d4-91c9-49f0-a3db-98a366076b28)block from the Functions menu:

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FIKDsiapUzUuPrAHHDWWo%2Fimage.png?alt=media&#x26;token=137f3e5c-0d57-4091-8c19-8cce1d832d5f" alt=""><figcaption><p>Functions menu in Blocks</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:&#x20;

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FxnLc6caBAAqVqJMpLiWw%2Fimage.png?alt=media&#x26;token=b0170f1b-3900-4f86-b40b-675f96b65204" alt=""><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://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FtKopS5aVhFMZTd7xqIsz%2Fimage.png?alt=media&#x26;token=30d6573a-7d92-4719-b5f3-be8bee8c02c5" alt=""><figcaption><p>New function block</p></figcaption></figure>

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FM9oIRxLYwpfBhufXUWjs%2Fimage.png?alt=media&#x26;token=77d95c08-4523-4e6b-a4aa-f6968564022b" alt=""><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://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2Fz6v6wiYQjCc1OPdFh5uP%2Fimage.png?alt=media&#x26;token=42bb0be5-e969-4df4-8557-ffda09a03bee" alt=""><figcaption><p>Example of a simple code within our function</p></figcaption></figure>

{% hint style="info" %}
The Starter Bot is able to climb the lower rung using the existing example program!
{% endhint %}

If the ![](https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2Fk7zr0SUBNrnWyrt3X2EY%2Fimage.png?alt=media\&token=3bd23c18-a128-4b5c-bbcb-4dcc179e8063) block is deleted this will remove the function from the "Functions" menu.

## Additional Function Example

By default, the Starter Bot's program only references each function once during our loop. The following is intended to be an additional example for reusing functions throughout a program and as an additional educational resource!

{% hint style="info" %}
This program can be tested on the Starter Bot or another robot using the same configuration file! Create a new OpMode to begin. Ours is called `FunctionsDemo`.

If you are using the Starter Bot, make sure the arm is adjusted to not drag while the robot drives autonomously.&#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://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F6pE9v3NqQ0yh9shJhxCV%2Fimage.png?alt=media&#x26;token=b023f195-65a8-4fae-a33c-9b1abd65b10e" alt=""><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://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F7GIWozmP1VlPCHTkeF1r%2Fimage.png?alt=media&#x26;token=eeef344b-0c73-4ffb-89ac-e2bb56d0399f" alt=""><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://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2FVe0zz7FVmmTVgKcQYrdR%2Fimage.png?alt=media&#x26;token=e4e6a052-b125-4c83-bb09-a3475daf7d2e" alt=""><figcaption><p>Function containing the code for driving and turning</p></figcaption></figure>

Now our loop may look like this:

<figure><img src="https://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F6SPIOH0ffCG4fKkyXvrv%2Fimage.png?alt=media&#x26;token=763f8383-92a9-4946-ba87-bf890300a1a4" alt=""><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://268621232-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MHCAE012xNfg1h3SM9v%2Fuploads%2F2Bc6stqUEtIzBS6xgyVN%2Fimage.png?alt=media&#x26;token=4a38652a-2799-434e-8ba4-d4fd2eecb1a8" alt=""><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!
