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.

6 Replies to “Using Arduino Functions Correctly, Make Your Code More Efficient”

  1. What if I just want to call an arg outside my loop? If I make another arg outside the loop, how do you get the Arduino to call it? Because I just get my argument name isn’t declared.

    void setup(){
    //setup parameters
    void loop(){

    void Argument1(){
    // args called from loop
    void Arguement2(){
    //args called from loop
    void Argument3(){
    //args called from loop

    1. Hi Anthony,
      Your code above is correct in theory. You are getting an error because you’ve spelled Argument2 as Arguement2 in the second function.

  2. Hi Michael, great explanation. Thanks very much.

    Can you tell me, how many defining parameters can you pass into a function?


    1. Hi Tim,

      I don’t think there is a limit, I’ve seen functions with 10+ parameters being passed to it.

      I would however avoid using more than 3 or 4. More than this and your function starts losing focus and could probably be separated into smaller or separate functions. This also makes your code more readable and is more testable for fault finding.

  3. please i have this arduino program that is not working as required. I have four laser receivers that i am monitoring its output signals with the arduino. when one laser receiver output signal is low or all becomes low, a function will be called to bring a relay on or blink a light. it functions alright but when the condition is not met i expect the light to stop blinking and the relay to go off but it doesnt. I blinks continuously. I need help please. Thanks

Leave a Reply

Your email address will not be published.