# multiple voidloop()

**URL:** <https://forum.arduino.cc/t/multiple-voidloop/343870>\
**Category:** Programming\
**Created:** [October 31, 2015, 3:55pm UTC](https://forum.arduino.cc/t/multiple-voidloop/343870 "2015-10-31T15:55:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredric58](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@Fredric58](https://forum.arduino.cc/u/Fredric58)\
**Post date:** [October 31, 2015, 3:55pm UTC](https://forum.arduino.cc/t/multiple-voidloop/343870/1 "2015-10-31T15:55:39Z")

</div>

hello, I have noticed reading through different coding that in some cases multiple loops are involved

the first one of course

void loop()

but later on there may be another like

void add\_this()

and then another

void XXXXXX()

why is it used?

can someone explain how they function within the code. or point to a tutorial?

thank you!

---

<div class="post-metadata">

**Author:** ![Whandall](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/whandall/32/222457_2.png) [@Whandall](https://forum.arduino.cc/u/Whandall)\
**Post date:** [October 31, 2015, 4:12pm UTC](https://forum.arduino.cc/t/multiple-voidloop/343870/3 "2015-10-31T16:12:38Z")

</div>

All of them are only functions, no loops.

The void loop() function is called by the main program that the IDE supplies, over and over.

If you look at main.cpp, which holds the 'real' program running on an Arduino, you will see the mechanism.  
(and some other special functions)

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

 initVariant();

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

```

---

<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:** [October 31, 2015, 4:13pm UTC](https://forum.arduino.cc/t/multiple-voidloop/343870/4 "2015-10-31T16:13:36Z")

</div>

The "voids" that you refer to are functions in the same way that pinMode() and digitalWrite() are functions. The difference is that they have been written by the programmer as part of the program as opposed to being part of the Arduino software itself. The word void indicates that the function does not return a value but they can be written to accept value(s) and return the result of calculations in which case the function name is preceded by the type of value that it returns.

Functions are used either to execute the same code at several points in the program and/or to separate sections of the code to make writing, understanding and debugging the code easier.

Do some research on C functions but in the meantime here is and example to experiment with.

```cpp
void setup()
{
  Serial.begin(115200);
}

void loop()
{
  for (int x = 0; x < 10; x++)  //the value oof x will go from 0 to 9
  {
    Serial.print("Two times ");
    Serial.print(x);
    Serial.print(" is ");
    Serial.println(timesTwo(x));  //call the function and print the value that it returns
    delay(1000);
  }
}

int timesTwo(int aNumber)  //this function will double the number that it is passed and return the result
{
  return aNumber + aNumber;
}

```

---

<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 31, 2015, 8:28pm UTC](https://forum.arduino.cc/t/multiple-voidloop/343870/5 "2015-10-31T20:28:09Z")

</div>

> Whandall:  
> All of them are only functions, no loops.
> 
> The void loop() function is called by the main program that the IDE supplies, over and over.
> 
> If you look at main.cpp, which holds the 'real' program running on an Arduino, you will see the mechanism.  
> (and some other special functions)
> 
> ```arduino
> int main(void)
> 
> ```

{  
init();

initVariant();

#if defined(USBCON)  
USBDevice.attach();  
#endif

setup();  
&nbsp; &nbsp;  
for (;😉 {  
loop();  
if (serialEventRun) serialEventRun();  
}  
&nbsp; &nbsp; &nbsp; &nbsp;  
return 0;  
}

> ```arduino
> 
> ```

\

And when you combine the above ( Excellent! ) and others explanations given here with this gem (attached) from " examples bare minimum " - with "run once / run repeatedly" explanation(!) you should get the 'rest of the story".

I always though that function names should be descriptive , but the looping is NOT the work of "loop" function -the for( ; ; ) IS!

Yes, standard Arduino team excuse applies here (again) - it would be too confusing for beginner to know the actual "main" function inner works.

void setup() {  
// put your setup code here, to run once:

}

void loop() {  
// put your main code here, to run repeatedly:

}

[/code]

---

<div class="post-metadata">

**Author:** ![Fredric58](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@Fredric58](https://forum.arduino.cc/u/Fredric58)\
**Post date:** [November 1, 2015, 12:07pm UTC](https://forum.arduino.cc/t/multiple-voidloop/343870/6 "2015-11-01T12:07:08Z")

</div>

yes I agree, to confusing for a beginner to understand the inner workings.

thank you for ya'lls input, sooooo much to learn

Mr Bob, can you change your code above to be three times x? thanks, side by side comparisons are a big help to me.

---

<div class="post-metadata">

**Author:** ![Fredric58](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@Fredric58](https://forum.arduino.cc/u/Fredric58)\
**Post date:** [November 1, 2015, 1:58pm UTC](https://forum.arduino.cc/t/multiple-voidloop/343870/8 "2015-11-01T13:58:24Z")

</div>

return aNumber + aNumber **+ aNumber;** that's what I missed.

thanks D

this would seem OK for (3) or (4) times a number, but "what if" they number was say 47 times X?

just curious.

---

<div class="post-metadata">

**Author:** ![Fredric58](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@Fredric58](https://forum.arduino.cc/u/Fredric58)\
**Post date:** [November 1, 2015, 3:02pm UTC](https://forum.arduino.cc/t/multiple-voidloop/343870/10 "2015-11-01T15:02:10Z")

</div>

yea, I have a lot of reading to do. i'll look back on this one day and it will be funny. as for now, just a whole lotta confusing!

oh btw, my community college has an intro to c++ class, just waiting for them to post the sched.

---

<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:33pm UTC](https://forum.arduino.cc/t/multiple-voidloop/343870/11 "2021-05-05T21:33:03Z")

</div>

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


