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:

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

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