In the previous article you saw the sketches, how they are structured and the elements that make it up. In this second article on programming with Arduino you will see a fundamental element: the functions and the role they play. At the end of this lesson, you will be able to recognize a function within a sketch, and you will have all the necessary knowledge on how to define them and use them for your purposes and projects.
The functions – Declaration and Call
In the previous article you saw the components that go to form the code of a sketch, such as blocks of instructions. In particular, you have also seen two functions that play the main role of a sketch structure: setup() and loop(). These two functions define two blocks of instructions each of which carries out a particular activity: setup () for the initialization of the program and loop () for the execution of the actual program in cycle.
Now the next step in programming a sketch is to define further functions. This means structuring the setup-loop starting scheme presented in the first lesson. This is done in order to create a more complex, more manageable and above all more efficient program.
First you will need to understand the correct use of a function within a sketch, or program.
The functions are actually blocks of instructions that are identified by a particular name (function declaration), such as setup and loop, and which are executed each time you use their name (function call) within the program.
Let’s see better in detail. The function declaration is nothing more than the assignment of a name to a block of instructions. Generally, an inherent or indicative name of the operations performed by the block of instructions is chosen.
From this moment on, all the instructions contained in the block can be executed only by writing the name of the function followed by the two round brackets, without having to write the block of instructions. This is the function call.
A function can be called endless times within the same program.
Let’s review the concept in general.
This structuring of functions within a program is not a concept restricted to the world of Arduino sketches, but it is a concept of informatics in general, born from many years of refinements and refinements on the most efficient structuring of a program. Functional programming has indeed been one of the biggest advances in programming that has led to much smaller and more efficient codes that are easier to understand and write. And consequently, the possibility of developing more complex programs.
Functions – Parameters and returning values
The functions are therefore blocks of instructions that are executed each time they are called. But it can often happen that the instructions inside need values defined or somehow processed outside the block in order to operate. We therefore speak of input values.
Other times, however, we need a possible result of a processing carried out within a block of instructions and then we talk about output values.
Let’s first look at how to manage outgoing values. These are generally referred to as returning values. To define them, you have to add the type of data before the function name.
Let’s put the case that within a function a series of mathematical calculations lead to a result we are interested in, which for example could be an integer. In the declaration of a function you will add the data type int just before the name of the function.
You will have to declare the value returned only in the declaration phase, then it will no longer be necessary to refer to it. During the call the returned value can be enclosed within a variable
result = function();
or ignore it
In this case, the returned value will be deleted from the memory.
In case the function does not return any value, then the void type must be defined at the beginning of the declaration.
So the value returned, whether present or not (void) must absolutely be specified during the declaration by its type immediately before the name of the function.
Now let’s see how the input values are managed in the declaration of a function.
Incoming values are referred to as parameters, and are shown in the parentheses immediately after the function name. The parameters are defined in sequence, specifying first the type of value and then a reference name. These parameters will then be used within the block’s instructions to perform certain operations.
The presence of parameters is not mandatory. In fact, some functions do not need values defined or calculated outside the block of instructions. In this case the round brackets are left empty.
In this article you have seen what the functions are and how they are used within a sketch through declaration and calls. In the next lesson we will introduce the constants that in the planning with Arduino assume an important role. We will see in fact how in this type of programming the constants are related to the concepts of electronics, with specific terms and functionalities.