Arduino IDE: knowing and using functions #9

Arduino IDE

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:

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.

funzioni

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:

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:

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:

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!

Simone Candido è un ragazzo appassionato del mondo tech nella sua totalità. Simone ama immedesimarsi in nuove esperienze, la sua filosofia si basa sulla irrefrenabile voglia di ampliare a 360° le sue conoscenze abbracciando tutti i campi del sapere, in quanto ritiene che il sapere umano sia il connubio perfetto tra cultura umanistica e scientifica.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.