# Functions

Are you tired of rewriting the same code over and over again? Well functions solve that! Functions in programming are reusable blocks of code that perform a specific task. Think of the steps to make a burger: cook patty, sandwich patty between two buns, and add vegetables. Now when you want to implement this, however, you have to arduously map out every substep. Instead of just cooking the patty, you have to grind up the filling, heat up the pan, etc. When you code out all these steps, the original 3 steps become increasingly hard to makeout. When you want to modify your code like by adding another patty, you have to scour your code to duplicate the original sandwich patty instructions. With functions, you don’t have to do that. Instead you can group the substeps into a single line of code. This way code can be easily duplicated and read. Now when you want to add an extra patty all you have to do is repeat the sandwich meat function.

Let's see functions in action.

Take the sample code:

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXdRAw5-Jx5cnIk2zIr_bs5bWF8YAY1VkTowQ3jZ92R2cKaIzvAZcOhAet5LFvjsZW8-P7w-AqyAnG_CG-YREUrvGbKeNPMxxRGsdYIubxgsA5PT4GFErUW8a2Q1GXP3AypFX0BJ4bbhid3gyXYq9E5srSc?key=f_c11F_mFqPBbiybrd0dpg)

At first glance it is difficult to understand what the series of motor controls do. Modifying the code is made even more confusing. When we break down the code, we can notice a pattern: set power on, sleep, set power off, report step is done. Some of the steps are even repeated like steps 3 and 4.

The first step is to put these sets of commands into functions. Using functions are simple, first name a function

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXc9fAR-I2OBELVtinFKxCAfer5vaccm4elzg54P4c2zDHzgaUaH3b0ER31jqAszd_PdtT0rBBOUaorlXv1zQftA6b2Hhgv8H8woRxRdeUEgY2IH-VFkyObe3GLHb1YARWnTejXJj32mkQUtvbW39cIjpaI?key=f_c11F_mFqPBbiybrd0dpg)

And then insert the code that represents that function.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXfqXytw6cg3Y2yk_Qq_v1lFwbkAVsgSLChHkMLOsD4w0xObPTc2V2frIB_S0-aihy4ZB4CHVOjp2Z8ZxF8QR7dCJlrc-6Du40y4nDqn9gPZJCwSgVmeHF-PE4BVDxzLGM2FcIOFGhngD1h1q4NI3a-VDIVF?key=f_c11F_mFqPBbiybrd0dpg)

Now that you have created a function, in order to you use you can simply insert the function’s block where the original code was.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXc5RdHz75OvZf03W1f2vfbKoiIWmRVJr_VcdeU0De7lbmcJsi1jsdG3HqSyycifrqljr_rUANLRjF2qCJqUWDpfDotlgTdqatZUjbb_hoN11qgVY2gxJCIk-WbahK41dL6KRZpw2VfvKGJpD4tI0eHS21UL?key=f_c11F_mFqPBbiybrd0dpg)

Now repeat for steps 2, 3, and 4.

![](https://lh7-rt.googleusercontent.com/docsz/AD_4nXfe512sOX3Aud-yTCTkf1BHCvI_6BC7UuJtJwb4z7x2y7YXv2JTULqAOYSiuH-KPqd-S9FaEAdRMIs8-s57a6D8DG_-3dqMfXja7O-QsAoi_4ZaxOkzAEd1hD4u0VpY1AfhTnmbd11aZQwHgOmk7m4fzLE?key=f_c11F_mFqPBbiybrd0dpg)

Now the code is much more readable! Remember to use functions when coding. Although they may seem unnecessary, they are vital in organizing more complex code. They will be used extensively in the advanced lessons to help break down more complicated sections.\
\ <br>


---

# 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://vrs-book-complete.gitbook.io/virtual-robot-simulator-official-guidebook/programming/basic-lessons/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.
