Execute Order 66…
Today we´re going to learn all about the Functions blocks. What are they? What do they do? Why do they have so many strange words? Read on to find out the answers:
Imagine that our program has to do something many times. For example, making two LEDs blink many times, or playing a long melody that we have created using more than 25 blocks. If we had to rewrite our melody every time we wanted it to be played, there would be so many blocks that we´d end up with square eyes!
So what if there was a way to create a tune just once, to give this action a name (such as “Playing the melody”) and then to play it using a single block? That is precisely what functions do! They help us to save time and to use less blocks. When we have a task that is repeated many times, or when we want to make our project clearer and more organised, we will create a function to execute a command every time we call it. But if you want functions to relieve headaches rather than to cause them, make sure you pay attention! So let´s get started.
For today´s examples, you will need:
- 1 x ZumBloq LED
- 1 x ZumBloq buzzer
- 1 x ZumBloq push-button
- 1 x ZumBloq continuous rotation servo
- A ZUM BT 328 controller board or one compatible with Arduino UNO
- A USB cable
I´ll be back…
The first thing to explain is that there are two types of functions in bitbloq: Functions without return and functions with return. Don´t worry, this doesn´t mean that the functions without return will disappear permanently when your execute them for the first time. “Without return” simply means that they will not return a value after execution.
When we want to create a function that makes mathematical calculations (adding, subtracting, adding again, multiplying, subtracting and multiplying, for example), what interests us in the end is the final value obtained. For this reason, we will use a function with return, which will return the final value of the calculations.
But when we want our function to carry out a series of actions that have nothing to do with the final value, such as playing that long melody, we will use the function without return.
The “function without return” and the “call function without return” blocks
As we have just explained, the function without return block (top left) is for grouping a series of blocks together and executing them when we call the function.
It could be said that when we create a function, we are creating a mini program and giving it name, so that when we call it, the main program “enters” into the function, runs through it entirely and then leaves it in order to continue with the main program (in this case, without returning any value).
That´s true, all of the blocks in our main program must be joined together. When we add a block to create a function, we are creating another small program on the side, which will be contained within the main program.
This will be a lot clearer when we analyse the next example.
Example using the function without return blocks
Program that makes a servo move on pressing the button, with flashing light and sound
As you will be an expert with the “If…do” control block by now, and you will have tried several examples that involve doing one thing on pressing the button and another if not, I´m sure you will understand the program if we remove the pink Blinking_with_sound blocks and the blocks on the right.
Let´s start by looking at the right side of the program. The first thing we see is the “function without return block”. This block is the one that starts to create a function without return. When we add it to our work area, the first thing we need to do is name it (as we did with the variables). Using this name, we will call our function using the “call function without return” block (as shown on the left side of the program). With the “function without return” block, we can create a program that we know will be used again and again. The block will get bigger and bigger as you add all the blocks you want to your function. And when we want our main program to carry out what we have programmed into the function, no matter how long it is, we will only need to put a “call function with return” block and change the name, as we would with the variables. As we can see in the example, each cycle of the program checks whether the button is pressed or not, in order to know whether to move the servo or to stop it. What´s more, before the servo block we are calling the function, so that every time we pass the pink “Blinking_with_sound” block, we are executing all the blocks of the function from top to bottom.
Two is better than one
We can also create programs with more than one function. How about a making the main program look nicer?
On pressing the button, the Flashing function will be executed, then the Melody function followed by the Flashing function.
We still need to look at one more type of function that will return a variable on finishing execution of the blocks. We will see how to use them later on; these are the “functions with return“. In order to finish mastering the functions, we will continue the course in the entry.