return

Description

Terminate a function and return a value from a function to the calling function, if desired.

Syntax

return; // used when the returned type of function is void return value; // used when the returned type of function is not void

Parameter Values

  • value: Allowed data types: any variable or constant type.

Example Code

A function to compare a sensor input to a threshold

int checkSensor() { if (analogRead(0) > 400) { return 1; } else { return 0; } }

The return keyword is handy to test a section of code without having to "comment out" large sections of possibly buggy code.

void loop() { // brilliant code idea to test here return; // the rest of a dysfunctional sketch here // this code will never be executed }

Testing Example

void setup() { Serial.begin(9600); printMessage(); } void loop() { } void printMessage() { Serial.println("Hello, ArduinoGetStarted.com"); return; Serial.println("Hi, ArduinoGetStarted.com"); }

The result on Serial Monitor:

COM6
Send
Hello, ArduinoGetStarted.com
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

As we can see, the text after return is not printed.

See Also

ARDUINO BUY RECOMMENDATION

Arduino UNO R3
Arduino Starter Kit
Disclosure: Some links in this section are Amazon affiliate links. If you make a purchase through these links, we may earn a commission at no extra cost to you.
Additionally, some links direct to products from our own brand, DIYables.

※ OUR MESSAGES