Subroutine in Arduino

Subroutine in Arduino

This tutorial will discuss working with subroutines by declaring functions and calling them in the main code in Arduino.

Subroutine in Arduino

In Arduino, we can declare functions and call them in the main code to work with subroutines. The code is executed line by line, and if we call a function at a line, Arduino will stop at that line and move to that function’s declaration.

It will execute the code present in the function and then move to the following line in the main code.

Example:

void setup()
{

}
void loop()
{
    //code
    Sub_1();
    //code
}
void Sub_1(){
    //code
}

When the Sub_1() function line is executed in the above code, the compiler will move to the Sub_1() function declaration and run its code.

It will return to the main code and move to the following line. We can declare functions with or without return types.

In the above code, we used the void keyword, which means the function will not return anything, but we can change the values of global variables in the function.

The variables defined at the start and are not enclosed by any function are called global variables.

Let’s define a global variable and change its value using a subroutine. See the code below.

int a = 5;
void setup()
{

}
void loop()
{
    //code
    Sub_1();
    //code
}
void Sub_1(){
    a = 10;
}

In the above code, the value of the global variable a will be changed after the subroutine Sub_1() is executed. We can print the variable’s value before and after the subroutine to check if it’s working or not.

We can also define functions with a return type that will return a value when they are called.

To define a function with a data type, we must first write the data type, the function name, and the parameters we want to pass inside the function.

For example, let’s define a function to find the sum of two integers and return the result as an integer.

void setup()
{

}
void loop()
{
    int a = 5;
    int b = 5;
    int c;
    c = Sub_1(a, b);
    //code
}
int Sub_1(int x, int y){
    int result = x+y;
    return result;
}

In the above code, when the function Sub_1() is called with the specific inputs, it will return the sum of the two inputs.

Note that the variables we passed inside the Sub_1() function are not the same as those we used inside the Sub_1() declaration. Still, we can use the same variables because they are not global.

The variables defined inside a function can only be used and changed inside that function. We can use the return keyword to return a value or a variable.

We can also use other data types to define a function like string, char, and long. If we don’t want to return any value or variable, we can use the void keyword to define the function.

Subroutines are helpful when we want to write a clean code or repeat a task multiple times in a code.

If we want to perform a task more than once in a code, we have to write the code multiple time, but we can make a function and put the code inside it and call it anytime in the main code.

It will reduce time and space and make the code clean and short.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook