# return in loop()

**URL:** <https://forum.arduino.cc/t/return-in-loop/348126>\
**Category:** Programming\
**Created:** [November 22, 2015, 5:29am UTC](https://forum.arduino.cc/t/return-in-loop/348126 "2015-11-22T05:29:47Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [November 22, 2015, 5:29am UTC](https://forum.arduino.cc/t/return-in-loop/348126/1 "2015-11-22T05:29:47Z")

</div>

Please move to Programming.

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

```cpp
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?  
.

---

<div class="post-metadata">

**Author:** ![MorganS](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/morgans/32/218718_2.png) [@MorganS](https://forum.arduino.cc/u/MorganS)\
**Post date:** [November 22, 2015, 5:59am UTC](https://forum.arduino.cc/t/return-in-loop/348126/2 "2015-11-22T05:59:21Z")

</div>

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

No and No.

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [November 22, 2015, 6:15am UTC](https://forum.arduino.cc/t/return-in-loop/348126/3 "2015-11-22T06:15:48Z")

</div>

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. 😢

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

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [November 22, 2015, 6:19am UTC](https://forum.arduino.cc/t/return-in-loop/348126/4 "2015-11-22T06:19:20Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [November 22, 2015, 6:23am UTC](https://forum.arduino.cc/t/return-in-loop/348126/5 "2015-11-22T06:23:00Z")

</div>

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 😴

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [November 22, 2015, 6:32am UTC](https://forum.arduino.cc/t/return-in-loop/348126/6 "2015-11-22T06:32:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Paul\_B](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/paul_b/32/216376_2.png) [@Paul\_B](https://forum.arduino.cc/u/Paul_B)\
**Post date:** [November 22, 2015, 7:54am UTC](https://forum.arduino.cc/t/return-in-loop/348126/7 "2015-11-22T07:54:11Z")

</div>

> 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](https://www.arduino.cc/en/Reference/Break)".

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [November 22, 2015, 8:10am UTC](https://forum.arduino.cc/t/return-in-loop/348126/8 "2015-11-22T08:10:54Z")

</div>

Depends how much code he wants to bypass. 😉

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [November 22, 2015, 10:38am UTC](https://forum.arduino.cc/t/return-in-loop/348126/9 "2015-11-22T10:38:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [November 22, 2015, 3:04pm UTC](https://forum.arduino.cc/t/return-in-loop/348126/11 "2015-11-22T15:04:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![econjack](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/econjack/32/1290082_2.png) [@econjack](https://forum.arduino.cc/u/econjack)\
**Post date:** [November 22, 2015, 3:27pm UTC](https://forum.arduino.cc/t/return-in-loop/348126/12 "2015-11-22T15:27:03Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [November 22, 2015, 3:28pm UTC](https://forum.arduino.cc/t/return-in-loop/348126/13 "2015-11-22T15:28:42Z")

</div>

please note the compiler will probably optimize this specific example to

```cpp
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

```cpp
#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.

---

<div class="post-metadata">

**Author:** ![GoForSmoke](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/goforsmoke/32/1127097_2.png) [@GoForSmoke](https://forum.arduino.cc/u/GoForSmoke)\
**Post date:** [November 22, 2015, 4:14pm UTC](https://forum.arduino.cc/t/return-in-loop/348126/15 "2015-11-22T16:14:47Z")

</div>

> 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) {};

---

<div class="post-metadata">

**Author:** ![robtillaart](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robtillaart/32/1098811_2.png) [@robtillaart](https://forum.arduino.cc/u/robtillaart)\
**Post date:** [November 22, 2015, 5:11pm UTC](https://forum.arduino.cc/t/return-in-loop/348126/16 "2015-11-22T17:11:49Z")

</div>

do not forget the interrupts...

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

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [November 22, 2015, 5:31pm UTC](https://forum.arduino.cc/t/return-in-loop/348126/17 "2015-11-22T17:31:22Z")

</div>

> She kicked me out of bed. 😢

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

But, then again maybe not 🕶  
.

---

<div class="post-metadata">

**Author:** ![nickgammon](https://avatars.discourse-cdn.com/v4/letter/n/45deac/32.png) [@nickgammon](https://forum.arduino.cc/u/nickgammon)\
**Post date:** [November 24, 2015, 7:58pm UTC](https://forum.arduino.cc/t/return-in-loop/348126/18 "2015-11-24T19:58:04Z")

</div>

> 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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:35pm UTC](https://forum.arduino.cc/t/return-in-loop/348126/19 "2021-05-05T21:35:21Z")

</div>

Closed on May 5, 2021, 9:35pm UTC


