Passing over variables to loop()

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

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.

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

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:

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

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?

1 Like

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

1 Like

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

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

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

// @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

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

void setup(void){

}
void loop(void){

}

Yes, that is incorrect code.

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

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

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)

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.

this is a function defined to accept arguments

this is not defined to accept arguments

void loop ();

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

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

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

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

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.

2 Likes

Looks like a standard function definition to me!