return

Descrição

Termina uma função e retorna um valor, caso desejado.

Sintaxe

return; return valor; // ambas as formas são válidas

Parâmetros

  • valor: qualquer variável ou constante de qualquer tipo de dado

Código de Exemplo

Uma função para comparar a saída de um sensor com um limiar

int checaSensor() { 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() { 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:

Newbiely | Arduino IDE 2.3.8
──
File
Edit
Sketch
Tools
Help
Arduino Uno
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'Arduino Uno' on 'COM15')
New Line
9600 baud
Hello, ArduinoGetStarted.com
Ln 11, Col 1
Arduino Uno on COM15
2

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

Ver Também

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