return in loop()

Please move to Programming.

In the example below, does using return mess up the stack?

void setup()
{
  pinMode(13,OUTPUT);
}

void loop()
{
  digitalWrite(13, !digitalRead(13));
  delay(100);
  while(1)
  {
    return;
  }
}

I should ask, does while(1) uses the stack?
.

Have you had your stacks messed up in the past? What specific problem do you think you are avoiding?

No and No.

I have used return; inside of loop() to bypass code.
ex:
if( continueState == true) return;
This works well for the times I use it.

Then at 4 AM I woke up and asked my wife if "while" pushes anything on the stack.
She kicked me out of bed. :cry:

Then I thought if there would ever be an case were using "return" inside loop would be a bad thing.
Hence the question.
Thanks and Thanks

Then at 4 AM I woke up and asked my wife if "while" pushes anything on the stack.

Why would it?

This was a bad dream, right?

I guess when you come to the closing brace in, while, it use a goto jmp to get back the the first line in while.

Very bad dream :sleeping:

You could find that out by disassembling the code, but yes, that is correct.

LarryD:
I have used return; inside of loop() to bypass code.

Correct me if I am wrong, but the word for which you are looking, is "break".

Depends how much code he wants to bypass. :wink:

The compiler knows about how to manage a stack, don't worry about that!

Paul__B:
Correct me if I am wrong, but the word for which you are looking, is "break".

break will get you out of a loop, but it won't get you out of loop().

It’s probably not a bright idea to return from loop() even if the compiler might correct that.

Looking at main.cpp, does return even get you out of loop()? Since loop() is within an infinite for loop, doesn't return just end up calling loop() again? What about a call to exit(), even though it's a little ugly.

please note the compiler will probably optimize this specific example to

void setup()
{
  pinMode(13,OUTPUT);
}

void loop()
{
  digitalWrite(13, !digitalRead(13));
  delay(100);
}

every return in loop() ends the call to loop() from main(), so before calling loop again some other things are done, e.g. checking Serial events

#include <Arduino.h>

int main(void)
{
 init();

#if defined(USBCON)
 USBDevice.attach();
#endif
 
 setup();
    
 for (;;) {
 loop();
 if (serialEventRun) serialEventRun();
 }
        
 return 0;
}

That said, some style guides dictate that every function should only have one place it exits.

LarryD:
Then at 4 AM I woke up and asked my wife

GoForSmoke:
probably not a bright idea

LarryD:
She kicked me out of bed. :cry:

econjack:
Looking at main.cpp, does return even get you out of loop()? Since loop() is within an infinite for loop, doesn’t return just end up calling loop() again? What about a call to exit(), even though it’s a little ugly.

I hadn’t looked. So that’s how they idiot-proof? I like!

IIRC exit() actually turns some AVR functions off.
If so it may use less power than
while (1) {};

do not forget the interrupts...

noInterrupts(); // prevents code running in interrupt mode
while(1);

She kicked me out of bed. :’(

You would think after 40 years there would be some indulgence.

But, then again maybe not :sunglasses:
.

GoForSmoke:
IIRC exit() actually turns some AVR functions off.
If so it may use less power than
while (1) {};

It turns interrupts off, that's about all.