# Passing over variables to loop()

**URL:** <https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007>\
**Category:** Programming\
**Created:** [April 30, 2025, 1:09pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007 "2025-04-30T13:09:01Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![domify80](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/domify80/32/1214767_2.png) [@domify80](https://forum.arduino.cc/u/domify80)\
**Post date:** [April 30, 2025, 1:09pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/1 "2025-04-30T13:09:01Z")

</div>

A question that roamed my mind for a quite a while,  
what is the advantage (if there is one) passing variables over from setup() to loop() vs. having global variables in a sketch?  
I know it might be easier using just global variables, but that is not the question here; easy or not.

Thanks

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [April 30, 2025, 1:16pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/2 "2025-04-30T13:16:03Z")

</div>

How does one "pass over" a variable from one function to another? Please post a code example to clarify the code constructs you're thinking of.

---

<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:** [April 30, 2025, 1:17pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/3 "2025-04-30T13:17:15Z")

</div>

> [@domify80](#):
>
> passing variables over from setup() to loop() vs. having global variables in a sketch?

`loop ()` has no arguments to accept value "passed" to it

---

<div class="post-metadata">

**Author:** ![alto777](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/alto777/32/317385_2.png) [@alto777](https://forum.arduino.cc/u/alto777)\
**Post date:** [April 30, 2025, 1:23pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/4 "2025-04-30T13:23:31Z")

</div>

Making a few assumptions about what you are getting at I searched

> global variables vs passing parameters

and this might be a thread to read:

> [@global variables vs passing arguments to functions...](https://forum.arduino.cc/t/global-variables-vs-passing-arguments-to-functions/658611):
>
> Greetings; I read somewhere, that global variables should be avoided in programming. Not sure why, but being a rookie, I just accepted this. Now I'm finding myself more and more often passing multiple variables to functions (most often void functions()). Are there any advantages for one method vs the other? I'm working with a mega 2560, just in case hardware plays a role. Cheers

and do the search yourself, read a few points of view.

There is no right or wrong, it's mostly a matter of circumstance.

HTH

a7

---

<div class="post-metadata">

**Author:** ![camsysca](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/camsysca/32/1015228_2.png) [@camsysca](https://forum.arduino.cc/u/camsysca)\
**Post date:** [April 30, 2025, 1:25pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/5 "2025-04-30T13:25:04Z")

</div>

The OP explicitly referenced loop() and setup(), so I'm presuming he has a context in mind that involves those functions. Let's not wander off into a general discussion of function arguments until he's posted some context, eh?

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 30, 2025, 1:33pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/6 "2025-04-30T13:33:21Z")

</div>

Have you read the language reference manual at least three times yet? If you have, now work through the Arduino Cookbook.

---

<div class="post-metadata">

**Author:** ![domify80](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/domify80/32/1214767_2.png) [@domify80](https://forum.arduino.cc/u/domify80)\
**Post date:** [April 30, 2025, 4:21pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/7 "2025-04-30T16:21:41Z")

</div>

Sorry, I made it only twice through the book and I couldn't find any reference to an explanation (or mention) of parameters to the `loop()` function.  
However I see the construct

```arduino
int main(void)
{
  init();
  setup();
  for (;;)
    loop();
  return 0;
}

```

(pg 25., 2nd Ed.)

That tells me that apparently it is not possible to pass over parameters to the `loop()` function.  
But I'm open for corrections.  
🙂

---

<div class="post-metadata">

**Author:** ![domify80](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/domify80/32/1214767_2.png) [@domify80](https://forum.arduino.cc/u/domify80)\
**Post date:** [April 30, 2025, 4:25pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/8 "2025-04-30T16:25:48Z")

</div>

As an explanation for all who asking me here is a code sample that is showing what I meant:

```arduino
// @file funcCall.cpp
// an example how to pass and receive parameters in function calls

// ***********
void myFunction (int p1, int p2) {
    Serial.println(p1);
    Serial.println(p2);
}
// end function myFunction() **** //

// ***********
void setup() {
    int var1 = 12;
    int var2 = 365;
    
    // how to call loop() with passing variables? perhaps [loop(var1, var2)]?
}
// end function setup() **** //

// ***********
void loop(int passvar1, int passvar2) {
    myFunction(passvar1, passvar2); // 'passvar1', 'passvar2' passed by value
   
}
// end function loop() ***** //

```

And btw., this code sample is **NOT** compiling successfully in the IDE v2.3.6

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 30, 2025, 4:26pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/9 "2025-04-30T16:26:05Z")

</div>

That is C code, NOT Arduino but your conclusion is correct, Arduino has no interface, a more correct coding would be

```arduino
void setup(void){

}
void loop(void){

}

```

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 30, 2025, 4:26pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/10 "2025-04-30T16:26:46Z")

</div>

Yes, that is incorrect code.

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 30, 2025, 4:27pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/11 "2025-04-30T16:27:50Z")

</div>

> [@domify80](#):
>
> twice through the book

The book is several inches thick, so what are you talking about?

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 30, 2025, 4:32pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/12 "2025-04-30T16:32:03Z")

</div>

> [@domify80](#):
>
> `void loop(int passvar1, int passvar2) {`

That sort of construct (it is actually varargs) is for command line invocation of a conventional C program on a mainframe computer. It also applies to smaller computers all the way down to a PC, but only under DOS (maybe?).

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 30, 2025, 4:38pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/13 "2025-04-30T16:38:19Z")

</div>

If you need to pass data into a program at run time there are many options. Today a web interface is often used but a preferences approach is also common. I am leaving out any sort of file system but do think a web interface to an eeprom storage might be an ok choice. There are also other memory types like fram to replace eeprom and for some boards a neat trick is a file loaded at startup via the Tools menu. (that last one is a very hazy memory)

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [April 30, 2025, 5:13pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/14 "2025-04-30T17:13:49Z")

</div>

> [@domify80](#):
>
> what is the advantage (if there is one) passing variables over from setup() to loop() vs. having global variables in a sketch?

There is no advantage as the former is impossible. If you want to modify variables in the `setup()` function and have them available in the `loop()` function, then they must be global.

---

<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:** [April 30, 2025, 5:36pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/15 "2025-04-30T17:36:57Z")

</div>

this is a function defined to accept arguments

> [@domify80](#):
>
> `void myFunction (int p1, int p2);`

this is not defined to accept arguments

```arduino
void loop ();

```

while you define one that does, the `main()` used by the Arduino IDE does not call it with arguemtns

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [April 30, 2025, 6:25pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/16 "2025-04-30T18:25:35Z")

</div>

> [@sonofcy](#):
>
> That is C code, NOT Arduino

Just a nit but Arduino code is C code. The Arduino IDE happens to do some preprocessing on the sketch.

---

<div class="post-metadata">

**Author:** ![sonofcy](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/sonofcy/32/1283617_2.png) [@sonofcy](https://forum.arduino.cc/u/sonofcy)\
**Post date:** [April 30, 2025, 6:43pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/17 "2025-04-30T18:43:35Z")

</div>

I thought Arduino was C++ but since I am not trained in C++ I have to rely on what others say.

---

<div class="post-metadata">

**Author:** ![ToddL1962](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/toddl1962/32/225060_2.png) [@ToddL1962](https://forum.arduino.cc/u/ToddL1962)\
**Post date:** [April 30, 2025, 6:48pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/18 "2025-04-30T18:48:21Z")

</div>

The compiler supports C++ in Arduino but most of the code I see is mostly vanilla C.

---

<div class="post-metadata">

**Author:** ![gfvalvo](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gfvalvo/32/1013044_2.png) [@gfvalvo](https://forum.arduino.cc/u/gfvalvo)\
**Post date:** [April 30, 2025, 6:58pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/19 "2025-04-30T18:58:41Z")

</div>

> [@ToddL1962](#):
>
> The compiler supports C++ in Arduino

More that than that. Unless you go out of your way to prevent it (by using a .c file), all Arduino is compiled as C++.

Moreover, the simplest `Serial.print()` statement is C++, not C.

---

<div class="post-metadata">

**Author:** ![cedarlakeinstruments](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cedarlakeinstruments/32/1128931_2.png) [@cedarlakeinstruments](https://forum.arduino.cc/u/cedarlakeinstruments)\
**Post date:** [April 30, 2025, 7:04pm UTC](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007/20 "2025-04-30T19:04:25Z")

</div>

> [@sonofcy](#):
>
> > [@domify80](#):
> >
> > `void loop(int passvar1, int passvar2) {`
> 
> That sort of construct (it is actually varargs)

Looks like a standard function definition to me!

[Next page](https://forum.arduino.cc/t/passing-over-variables-to-loop/1378007.md?page=2)
