Welcome back to our programming tutorial using the Arduino IDE. Today we will learn what functions are, and how to work with them.
You can take a look at the previous chapters of the course here:
- Arduino IDE: composing songs with an array #8.1
- Arduino IDE: what is an array or a vector #8
- Arduino IDE: RGB LED, for, while, do while loops #7
- Arduino IDE: for loops against while / do while #6
- Arduino IDE: while and do while loops #5
- Arduino IDE: turn on LEDs using a button (if) #4.1
- Arduino IDE: the conditional construction IF #4
- Arduino IDE: arithmetic and logical operators #3
- Arduino IDE: variables, constants and macros #2
- How can I use Arduino IDE to write a program? #1
- Arduino IDE: what it is and how it works #0
In the previous articles of this guide we have seen programs showing the solution of simple problems. When the level of complexity of the problem increases, it is better to divide the program into subprograms.
It is basically a mater of defining the fundamental composition parts of the program, and then proceeding to detail each single part. This way, both the problem and the program are broken down into functionally independent modules.
In the Arduino IDE programming language, such subroutines are represented through functions.
Each function receives a set of arguments called parameters as input, and returns a value.
In the Arduino IDE, functions are used to avoid replicating portions of code; in fact, invoking a subprogram means sending the relative portion of the code in execution. A single function can be invoked multiple times.
Before using a function, you must declare it as you do for variables. Unlike variable declaration, though, it is not necessary to declare a function before it can be invoked, but it is strongly recommended for documentation purposes, and mandatory whenever the function is implemented after the main(). The declaration allows us to understand only how to invoke the function: it specifies how many arguments it takes as an input, their type, and the type of the return value.
The syntax is therefore the following:
1 |
<return_type> function_name ([<parameter_list>]) |
The first element of the declaration is the type of the return value. It is followed by the name of the function, and a list of zero or more parameters inside the round brackets, separated by commas. For each parameter, the type and name are indicated. The declaration is terminated by the “;” symbol. The name chosen for the function should give an idea of the type of operation it performs.
Here are some examples:
- double cube (float c);
- int max (int x, int y);
- int minimum (double x, double y);
- void compute_area (float a, float b);
The functions are defined in this way:
1 2 3 4 5 |
<return_type> function_name ([<parameter_list>]) { <instructions> return return_value; } |
The function call, odften called “invocation”, is an instruction that allows the execution of the function. It is done inside the main() or loop(). It is necessary to specify the name of the function followed by the eventual list of the parameters, in round brackets, through their assigned values. Even if there are no parameters, the parentheses following the function name are mandatory:
1 |
function_name (list_of_parameter_values); |
The compiler checks the correspondence between the parameters specified in the function definition and the values passed to the function during the compilation step.
The parameters are also called arguments: they represent the data “passed” to the function at the time of the call.
The formal parameters are those defined in the header. The current parameters are those used in the actual call and can have a different name from the formal parameters. The two types of parameters must correspond IN NUMBER (if the function has two parameters, you must pass them two), IN TYPE (e.g. if the first is int and the second char, you must pass them to him in this way) and IN ORDER (e.g. if the parameters are first the base and then the exponent, it is necessary to follow the same order)
A function of type void is a function that returns no values. In this case the return statement is omitted. The void function is also called a procedure, as it does not return any values but executes instructions inside the function. The name “procedure” is borrowed from older programming languages, such as Pascal.
Follow us to keep yourself updated with the news!