Dividing your code up into functions or methods allows you to perform similar actions multiple times without having to duplicate the code. Functions are essentially modular blocks of code which perform a defined task and then return to the area of code from where the function was initially called.

This exercise assumes that you have a basic understanding of the Arduino IDE and how to upload a sketch, if you haven’t done this before, have a look at our guide to writing and uploading your first Arduino sketch.

The advantage of using functions come in when you need to debug or modify your code. Say you use the number of times an LED blinks throughout your code to readout different values and you decide that you want the LED to blink more quickly. If you haven’t used methods, you’d have to go and change the rate each time you’ve asked the LED to blink whereas with a method, you only need to change a single line.

The predefined sections of code setup() and loop() are functions which your Arduino has been programmed to recognise. There are essentially four types of functions which you can use, we’ll go through each one, how to define it and how to call it in the order of increasing complexity.

Basic Void Functions

To demonstrate the use of functions, we will use the Arduino’s on board LED and the Serial monitor function later on to display the returned results.

The function gets defined outside of any of the other functions in the code in a similar way to the setup() and loop() functions. The basic structure is:

The function is called within the code using the syntax:

The function can be called from within the setup() function, the main loop() function or from within other functions in the code.

Here is an example of a function which blinks the on board LED once. The function is called up in the main loop to blink the LED three times.

Basic Return Functions

The placeholder void before the function name indicates that nothing is being returned by the function. It is often useful for a function to be able to return some type of variable to your main method. The type of variable which the function returns then replaces the void before the function name. In this example we will call a function to blink the LED a random number of times, this number will be returned to the main method and displayed in the Serial window.

The function is now declared as:

As is called using:

The complete sketch now becomes:

Passing Parameters To Void Functions

To increase the functionality of our function, we can pass it parameters from our main block of code. In order to pass the function parameters, we first need to tell the function what type of parameters to expect and assign variable names to them, this is done by defining the parameters in the same way you do within the code but now in the brackets after the function name. We can pass the function any variable types allowed by the Arduino IDE.

Now, when we call the function, we will need to pass it the variable it is expecting in the same data type. You can do this by passing it actual values or passing it variables of the same data type.

Call your function using values:

Call your function using variables:

The complete sketch would now look like this:

Passing Parameters To Return Functions

Finally, we can combine the two and pass parameters to a function which also returns a variable. In this example, we will call a function which then blink the LED a random number of times between zero and a limit we send to the function, the random number will then be displayed on the serial monitor.

The complete sketch is now:

And that is everything you need to know about using functions with an Arduino. If you have any questions or suggestions, let me know in the comments section below.