# Is it dangerous to use return; inside loop()?

**URL:** <https://forum.arduino.cc/t/is-it-dangerous-to-use-return-inside-loop/891715>\
**Category:** Programming\
**Created:** [August 2, 2021, 7:05pm UTC](https://forum.arduino.cc/t/is-it-dangerous-to-use-return-inside-loop/891715 "2021-08-02T19:05:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bradley2020](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@bradley2020](https://forum.arduino.cc/u/bradley2020)\
**Post date:** [August 2, 2021, 7:05pm UTC](https://forum.arduino.cc/t/is-it-dangerous-to-use-return-inside-loop/891715/1 "2021-08-02T19:05:15Z")

</div>

Hi everyone, I'm writing a new piece of code and I'm trying to avoid using nested `if()` statements to help readability. For example, when creating functions, I avoid this:

```cpp
void myFunction(int var){

  //if var is positive, execute myFunction()
  if(var > 0){
  
    //execute all of myFunction() inside if() statement, could lead to nested if statements!

  }

  return;

}

```

in favour of this:

```cpp
void myFunction(int var){

  //if var is not positive, return, i.e. don't execute myFunction()
  if(var <= 0)
    return;
  
  //execute all of myFunction(), except this time we're not inside a big if() statement!

  return;

}

```

plus I've also been told by a friend who's a software developer/programmer that it's good practice to avoid nesting `if()` statements.

This is fine for functions, but for the Arduino loop statement, I was wondering if I could do the same. I'm aware that the main.c file has defined the `main()` function in Arduino as:

```cpp
int main(void)
{
	init();

	initVariant();

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

```

so I know that if I return from `loop();` then it will just execute again as it is inside an endless `for(;;)` loop. so, for example, when I run this code:

```cpp
bool someVariable = false;

void setup(){
  Serial.begin(115200);
  Serial.flush();
}

void loop(){
  
  delay(1000);
  Serial.println(F("Hello!"));

  if(!someVariable)
    return;

  Serial.println(F("Hi!"));

}

```

the only message I see in the Serial monitor is "Hello", and the `Serial.println(F("Hi!"));` will never execute due to the `if()` statement.

So, my questions are, am I correct in how I have described the way the final code will run? and if so, is it bad practice or potentially dangerous to rely on `return;` inside `loop()`?

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [August 2, 2021, 7:11pm UTC](https://forum.arduino.cc/t/is-it-dangerous-to-use-return-inside-loop/891715/2 "2021-08-02T19:11:11Z")

</div>

If you want to quit any function immediately and return to where it was called then you use return;

loop() is just a function called from main() and as such it is legitimate to `return;` from it and have it called again from the start

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [August 2, 2021, 7:11pm UTC](https://forum.arduino.cc/t/is-it-dangerous-to-use-return-inside-loop/891715/3 "2021-08-02T19:11:58Z")

</div>

of course not

```cpp

void loop ()
{
    if (! Serial.available ())
        return;

    ...
}

```

---

<div class="post-metadata">

**Author:** ![Coding\_Badly](https://avatars.discourse-cdn.com/v4/letter/c/f14d63/32.png) [@Coding\_Badly](https://forum.arduino.cc/u/Coding_Badly)\
**Post date:** [August 2, 2021, 7:16pm UTC](https://forum.arduino.cc/t/is-it-dangerous-to-use-return-inside-loop/891715/4 "2021-08-02T19:16:45Z")

</div>

> [@bradley2020](#):
>
> ...will never execute due to the `if()` statement...

As a fun bonus, if `someVariable` is `static` or `const` the compiler will eliminate the `if` and the second `println` because it's dead code.

---

<div class="post-metadata">

**Author:** ![bradley2020](https://avatars.discourse-cdn.com/v4/letter/b/e8c25b/32.png) [@bradley2020](https://forum.arduino.cc/u/bradley2020)\
**Post date:** [August 2, 2021, 7:21pm UTC](https://forum.arduino.cc/t/is-it-dangerous-to-use-return-inside-loop/891715/5 "2021-08-02T19:21:09Z")

</div>

ah cool! that's cool that the compiler actually figures out how a code will run, I thought it just eliminated functions and variables that were never used

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [August 2, 2021, 8:31pm UTC](https://forum.arduino.cc/t/is-it-dangerous-to-use-return-inside-loop/891715/6 "2021-08-02T20:31:26Z")

</div>

But only to a limited extent. It can not grasp semantics, only look for definite patterns that have easily predictable outcomes. Here there is no magic, though - surely if it can eliminate a function, it can eliminate a code block.

A logical test of a constant value, is a pretty easy thing to detect, if you see what I mean...

---

<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:** [November 30, 2021, 8:31pm UTC](https://forum.arduino.cc/t/is-it-dangerous-to-use-return-inside-loop/891715/7 "2021-11-30T20:31:37Z")

</div>

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.
