Arduino

Arduino Return Function – How to Use Return in Arduino

One of the key concepts in Arduino programming is functions, which allow you to write reusable code and create modular programs. Functions can also return values, which can be used to control the flow of your program or make calculations.

In this article, we’ll explore the Arduino return function and how it can be used to return values from a function.

Arduino return

The Arduino return is used to exit the function and return a value to the caller. The value that is returned can be used by the caller to make further calculations or to control the flow of the program. The return function is an important aspect of programming as it allows you to reuse code and create modular programs.

return Syntax

In Arduino, a return function has the following syntax:

[return_type] function_name([parameters]) {
// function body
return [return_value];
}

Where:

return_type is the data type of the value which is returned. It can be any data type, such as int, float, char, boolean, etc. The return_type is void if no value is returned.

function_name is a name defined for a function whose value is to be returned.

parameters are the values that can be passed to the function. The parameters are optional if no parameters are there leave them blank. Each parameter consists of a data type followed by a parameter name. Multiple parameters are separated by commas.

return_value is the value that will be returned by the function. The value which is returned must have the same data type that matches with the return_type specified inside function.

Here is an example of a return function in Arduino that takes two parameters and returns an integer value:

int addTwoNumbers(int num1, int num2) {
int result = num1 + num2;
return result;
}

In this example, the function addTwoNumbers takes two integer parameters num1 and num2, adds them together, and returns the result as an integer value. To call this function in your code, you would write something like:

int a = 10;
int b = 20;
int c = addTwoNumbers(a, b); // c will be equal to 30

Return Any Data Type Using Arduino return

The return function in Arduino can be used to return any data type, including integers, characters, and strings. To return a value its data type must be declared inside the function declaration. For example, if an integer is need to be returned, you must declare the function as follows:

int myFunction() {
// code
return value;
}

In this example, the function returns an integer value. To call the function, return value is needed to be assigned to a variable as below:

int result = myFunction();

The value that is returned from the function can be used for further calculations or for controlling the flow of the program. For example, you can use the return value to control the flow of an if statement:

if (result == 0) {
// code
}

The above code will only execute if the condition is true means the returned value is equal to 0.

Return a String Using Arduino return Function

Another use of the return function in Arduino is to return a string. To return a string, you must use the String class in Arduino. For example:

String myFunction() {
return "Hello, World!";
}

In this example, the function returns a string value. To call the function assign a variable for returned value:

String result = myFunction();

The value of the result variable is now equal to “Hello, World!”.

Return Multiple Values Using Arduino Return Function

The return function in Arduino can also be used to return multiple values. To do this, you must use an array or a structure to store the values. For example, to return two integer values following syntax can be used:

void myFunction(int &a, int &b) {
a = 1;
b = 2;
}

In this example, the function takes two integer values as input and returns them to the caller. To call the function, you must pass two variables to the function:

int a, b;
myFunction(a, b);

Here a is equal to 1 and b has value 2.

Arduino Example Code

Here is an example program that demonstrates the use of the return function in Arduino:

int addTwoNumbers(int num1, int num2) {
int result = num1 + num2;
return result;
}
void setup() {
Serial.begin(9600);
int a = 10;
int b = 20;
int c = addTwoNumbers(a, b);
Serial.println("The result of adding two numbers is: " + String(c));
}
void loop() {
// Leave the loop empty
}

In this program, the addTwoNumbers function takes two integer parameters num1 and num2 and returns the sum of those numbers as an integer. The setup function initializes the serial communication and calls the addTwoNumbers function, passing the values 10 and 20 as parameters. The result is stored in the c variable, and then it is printed to the serial monitor using the Serial.println function. In the loop function, there is no code, but you can add your own code if necessary.

This is just one example of how you can use the return function in Arduino. You can use the return function to return a value from a function in many ways, depending on your needs.

Output

Total sum of two numbers which is 30 is displayed in the serial monitor output.

Conclusion

Arduino return function provides a powerful and flexible way to return values from a function to the caller. The return function can be used to return any data type, including integers, characters, and strings. The return function allows you to reuse code and create modular programs that are more efficient and readable.

About the author

Kashif

I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.