In this lesson we will be taking a look at the use of functions. Once we have learnt how to program functions and after we have studied communication via serial port and using variables, we will be ready to create real programs that interact with peripherals and communicate with other devices.

Functions for what?

Functions are simply lines of code grouped together to carry out a specific task, which we give a name. They are not strictly necessary, as this code can be inserted at any point required in the program, however, they do allow the code to be developed in a cleaner, more organised way.

Functions serve three fundamental purposes:

  • Creating cleaner, more organised code that´s easier to maintain
  • Promoting reuse of code using function libraries
  • Promoting improvement and growth of code

In effect, functions facilitate the work of the developer enormously, not only when developing their own code, but also essentially when attempting to use code developed by others.

The “Hello World (Hola Mundo)” function

How would the most basic function, limited to printing “Hello World” via serial port, be?

Now we will analyse it.

  • void – this word is used because the function doesn´t return anything, which means it doesn´t produce any results. This will become clearer when we make a function that does produce a result.
  • holamundo – this the name we have given the function which is helloworld in Spanish, but we could have given it any name.
  • ( ) – any parameters would have been put in brackets, but as there aren´t any in this case, it has left it blank. This will also become clearer when we create a function that does use parameters.

What would a program be like using the holamundo function?

What does this program do? Try it yourself, just see that this program is identical to this one.

Using parameters

Let´s imagine a function that we want to return the result of adding numbers a and b via serial port. As we don´t know the value of a and b at the time of programming, we pass them as a parameter (which could be a reading from a sensor).

Now let´s analyse it.

  • void – this function returns nothing again.
  • sumar – (add) is the name of the function.
  • (int a, int b) – these are the two parameters taken by this function to make the calculations. a and b are local variables of the function so they can only be used within this function.

What would a full program look like?

This function would send string 9 via serial port.

An INCORRECT use of variables in functions

Now we will set a code that compiles and functions, however, you should never do this.

Although this program compiles and functions, it´s basically a programming malpractice. Why? We are declaring the global variables a and b, which can be used in any part of our program (because they are global). Then we declare the add function, without any parameters, to add a and b. It will use the values of a and b each time, because they are global. Finally, we assign a value to a and b and add them up. So far, so good. However, we should not use global variables within functions. In fact, we should pass the required values using parameters (as we have done previously). Although it will function, it is incorrect, because it doesn´t meet our aforementioned objectives of using, reusing, developing and maintaining clean code.

Now we will return a value

Finally, we will make a function which, in addition to doing things,  returns a value, such as the product of two numbers. As you will all be expecting, the function and the program it uses would be like this:

Have a go yourself and you will see how it works.

An exercise

Now have a go at creating a function that stops a program from being executed until something is received via the serial port.  We will use this function later. It should basically leave the loop function waiting until something arrives via the serial port. It then reads the command (without responding) and keeps executing the program as normal. This is useful, for example, when our program is waiting to receive commands via Bluetooth, in order to know what to do. It waits until it arrives and then acts accordingly.

I will leave you with a possible solution in the next video:



Next time we will learn how to light up LEDs and start having fun with them.