# Restarting the "void loop()"  out of an other function ~ HOW?

**URL:** <https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009>\
**Category:** Programming\
**Created:** [October 14, 2016, 4:15pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009 "2016-10-14T16:15:08Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![SomeBlackFire](https://avatars.discourse-cdn.com/v4/letter/s/cc9497/32.png) [@SomeBlackFire](https://forum.arduino.cc/u/SomeBlackFire)\
**Post date:** [October 14, 2016, 4:15pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/1 "2016-10-14T16:15:08Z")

</div>

Hey guys,  
well the title pretty much says what i am planning to do...  
I was thinking about something like that:

```cpp
void FUNCTION() {
   some code here...
   if(condition) {
      "restart void loop"
   }
   some code there....
}

void setup() {
   some things for the setup...
}

void loop() {
   some code here....
   FUNCTION();
   some code there....

```

What do you think? Any ideas, how i could realize this?  
some of you will say: "you could just write your function in the loop and use something like a break or a return..."  
well, unfortunately this isn´t an option in my case 😕

---

<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:** [October 14, 2016, 4:16pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/2 "2016-10-14T16:16:37Z")

</div>

You could make FUNCTION return a boolean, and test that in loop(), and if true, simply return from loop()

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [October 14, 2016, 5:00pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/3 "2016-10-14T17:00:01Z")

</div>

You should not think of using a function which is called from loop() to cause loop() to restart without allowing loop() to complete. If you could do that you would almost certainly cause your program to crash after a few iterations.

As @AWOL has said you can include code in loop() to make it terminate (return) if a variable is set by your function.

...R

---

<div class="post-metadata">

**Author:** ![anon68129940](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anon68129940](https://forum.arduino.cc/u/anon68129940)\
**Post date:** [October 14, 2016, 5:55pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/4 "2016-10-14T17:55:30Z")

</div>

Hi,

One option, if you really really had to do this, would be to find the place in the library code where loop() is called in the first place. You could modify that so when loop() returns you simply call loop() again. If you need it to be conditional then you might set up a conditional statement and variable to test before calling it again, or if there is a true program end then just enter an infinite loop that never ends and does not do anything rather than returning from loop() for the very last time.

There is most surely another way to do what you have to do though. For example, just encapsulate the code you have to repeat inside of loop() or in a function you call whenever you need to repeat it. That should work just as well and you dont have to modify anything outside of your own application code.

void loop()  
{  
Code\_I\_Have\_To\_Repeat();  
}

or something like that 🙂

---

<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:** [October 14, 2016, 6:16pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/6 "2016-10-14T18:16:32Z")

</div>

> MrAl:  
> Hi,
> 
> One option, if you really really had to do this, would be to find the place in the library code where loop() is called in the first place. You could modify that so when loop() returns you simply call loop() again.

See reply #1

---

<div class="post-metadata">

**Author:** ![6v6gt](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/6v6gt/32/225488_2.png) [@6v6gt](https://forum.arduino.cc/u/6v6gt)\
**Post date:** [October 14, 2016, 6:38pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/7 "2016-10-14T18:38:45Z")

</div>

Arduino's main() function looks something like this:

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

    setup();

    for (;;) {
        loop();
    }

    return 0;
}

```

If you can't work within this structure, then you can override it with your own version of main().

---

<div class="post-metadata">

**Author:** ![Jiggy-Ninja](https://avatars.discourse-cdn.com/v4/letter/j/8edcca/32.png) [@Jiggy-Ninja](https://forum.arduino.cc/u/Jiggy-Ninja)\
**Post date:** [October 14, 2016, 7:11pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/8 "2016-10-14T19:11:01Z")

</div>

> 6v6gt:  
> If you can't work within this structure, then you can override it with your own version of main().

Do not take this advice.

I cannot conceive of _any_ reason that a microcontroller would need to have something other than the setup/loop idiom. If SomeBlackFire doesn't know how to make it work within that frame, I doubt there's any way they could make it work mucking around with the main function.

---

<div class="post-metadata">

**Author:** ![jimLee](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jimlee/32/214865_2.png) [@jimLee](https://forum.arduino.cc/u/jimLee)\
**Post date:** [October 14, 2016, 11:31pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/9 "2016-10-14T23:31:46Z")

</div>

When I start thinking like this (quick clever tricks that should solve..), it's always a sign that I need to rethink the basic logic for solving the problem.

Also remember; if you recall loop(); Its going to carve out memory from your stack. And if you do it too much, without unwinding it, your memory is gone.

Rethink your program. It's always best to find a method that fits what the designer intended.

-jim lee

---

<div class="post-metadata">

**Author:** ![PaulMurrayCbr](https://avatars.discourse-cdn.com/v4/letter/p/74df32/32.png) [@PaulMurrayCbr](https://forum.arduino.cc/u/PaulMurrayCbr)\
**Post date:** [October 15, 2016, 6:43am UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/10 "2016-10-15T06:43:14Z")

</div>

You don't restart the (ugh) "void loop". You write a loop that, as it runs normally, has the behaviour that you want. Rather than restarting when - for instance - a button is pressed, your write code that as part of it's operation watches the button and does whatever needs doing when it is pressed. If that involves resetting things to zero, you reset things to zero.

---

<div class="post-metadata">

**Author:** ![anon68129940](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anon68129940](https://forum.arduino.cc/u/anon68129940)\
**Post date:** [October 15, 2016, 7:58am UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/11 "2016-10-15T07:58:20Z")

</div>

> AWOL:  
> See reply #1

Hi there,

Oh yes, i guess you meant the same thing then.

I guess most people are advising against this anyway now with the idea that there are better ideas that maintain the original basic structure of the Arduino code framework. I tend to believe that too because some libraries may come along that take that for granted.

In Windows programming we have one main() function too, but there doesnt seem to be any rule like loop() to follow so it's a bit more versatile. But there it would not be uncommon to write a new main() function for every new application while in Arduino Land it would be more uncommon. Would it _really_ be a problem in Arduino though? I guess only time would tell 🙂

One problem i can think of offhand is that someone downloads the 'sketch' for that rewritten Arduino program and it does not run correctly and they cant figure out why. It turns out that they would also have to be sure to download the new 'main()' function as well or they would not get that significant piece of code that has the multiple loop() calls. It may take some time to figure out that they need that new main() function as well because that is not usually required in 'normal' sketchs.

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [October 15, 2016, 8:05am UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/12 "2016-10-15T08:05:42Z")

</div>

> MrAl:  
> But there it would not be uncommon to write a new main() function for every new application while in Arduino Land it would be more uncommon. Would it _really_ be a problem in Arduino though? I guess only time would tell 🙂

How would it make any difference?

It would just change the OP's question to Restarting the "void main()" out of an other function ~ HOW?

And the answers would be pretty much the same.

...R

---

<div class="post-metadata">

**Author:** ![anon68129940](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anon68129940](https://forum.arduino.cc/u/anon68129940)\
**Post date:** [October 15, 2016, 8:31am UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/13 "2016-10-15T08:31:37Z")

</div>

> Robin2:  
> How would it make any difference?
> 
> It would just change the OP's question to Restarting the "void main()" out of an other function ~ HOW?
> 
> And the answers would be pretty much the same.
> 
> ...R

Hi,

Did you read the last paragraph of my previous post?

Also, it's not restarting main() it is rewriting main().

---

<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:** [October 15, 2016, 8:44am UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/14 "2016-10-15T08:44:34Z")

</div>

Did you read reply #1?  
It does what the OP requested - the only deviation from the spec (such as it was) is that FUNCTION returns a value.

No rewrite of main, no rewrite of loop.

---

<div class="post-metadata">

**Author:** ![anon68129940](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anon68129940](https://forum.arduino.cc/u/anon68129940)\
**Post date:** [October 15, 2016, 9:03am UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/15 "2016-10-15T09:03:18Z")

</div>

> AWOL:  
> Did you read reply #1?  
> It does what the OP requested - the only deviation from the spec (such as it was) is that FUNCTION returns a value.
> 
> No rewrite of main, no rewrite of loop.

Hi,

Sure, that's great. I guess people were looking for other solutions also.  
Funny i never had to do any of this i just write inside loop mostly except for interrupts 🙂

---

<div class="post-metadata">

**Author:** ![Robin2](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/robin2/32/216045_2.png) [@Robin2](https://forum.arduino.cc/u/Robin2)\
**Post date:** [October 15, 2016, 9:35am UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/16 "2016-10-15T09:35:21Z")

</div>

> MrAl:  
> Did you read the last paragraph of my previous post?
> 
> Also, it's not restarting main() it is rewriting main().

I know you were referring to "rewriting".

But I was trying to convey the idea that the OP does not have your depth of expertise and if the visible top level function was main() rather than loop() he would just transfer the same question to it.

Also his question (IMHO) does not need to be solved by rewriting main(). It needs to be solved by understanding how to use functions.

...R

---

<div class="post-metadata">

**Author:** ![anon68129940](https://avatars.discourse-cdn.com/v4/letter/a/e9a140/32.png) [@anon68129940](https://forum.arduino.cc/u/anon68129940)\
**Post date:** [October 15, 2016, 7:50pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/17 "2016-10-15T19:50:28Z")

</div>

> Robin2:  
> I know you were referring to "rewriting".
> 
> But I was trying to convey the idea that the OP does not have your depth of expertise and if the visible top level function was main() rather than loop() he would just transfer the same question to it.
> 
> Also his question (IMHO) does not need to be solved by rewriting main(). It needs to be solved by understanding how to use functions.
> 
> ...R

Hi,

Yes i agree, and you can tell by the way i said the very same thing previously 🙂

I also stated that i never had to do that and i think that anyone that thinks they do need to do that just needs to look at a few more sketches or something to see that none of them do that, nor do they need to do that. I think everyone here made that very clear now too 🙂

---

<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:** [October 15, 2016, 9:26pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/18 "2016-10-15T21:26:16Z")

</div>

> SomeBlackFire:  
> Hey guys,  
> well the title pretty much says what i am planning to do...  
> I was thinking about something like that:
> 
> ```arduino
> void FUNCTION() {
> 
> ```

some code here...  
&nbsp; if(condition) {  
&nbsp; &nbsp; &nbsp; "restart void loop"  
&nbsp; }  
&nbsp; some code there....  
}

void setup() {  
&nbsp; some things for the setup...  
}

void loop() {  
&nbsp; some code here....  
&nbsp; FUNCTION();  
&nbsp; some code there....

> ```arduino
> 
> What do you think? Any ideas, how i could realize this?
> some of you will say: "you could just write your function in the loop and use something like a break or a return..."
> well, unfortunately this isn´t an option in my case :/
> 
> ```

The way your code ought to be structured is that loop() calls all the other functions, directly or  
indirectly and they all return (in a timely manner). So you'd just return back into the loop.

Restarting loop is a red-herring, loop is always restarted constantly, its the event loop. It should be  
running 1000's of times a second.

---

<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, 10:07pm UTC](https://forum.arduino.cc/t/restarting-the-void-loop-out-of-an-other-function-how/413009/19 "2021-05-05T22:07:38Z")

</div>

Closed on May 5, 2021, 10:07pm UTC


