How to get rid of global variables?

Hi, i am working on school project and i need little help. In my code i am using global variables, bcs i need to remember some variables after loop, but my teacher hate it. Can i replace them with something? Or is it possible to use eeprom to write variable after use function and in the start read? Or is it just stupid thing and i should keep them global?

Thank you for help

depends. post your code

If you make a variable in a function static it will retain its value from one iteration to another.

Yes, but static is close same to global.

Not if the variable is locally declared. It is invisible outside of the function in which it is declared.

I would like to, but it is so big. I can post just part of it.

void stopwatch()
{
   long static startTime, timerMode = 0;
   float static actualTime = 0.0, timeHistory = 0.0;
   lcd_set_cursor(0,1);
   lcd_print("Posledny: ");
   lcd_print(timeHistory);
   lcd_set_cursor(0,0);
   lcd_print("Stopky: ");
   if(digitalRead(BTN_ENTER_PIN) !=0)
   {
      startTime = millis();
      timerMode++;
      delay(400);
   }
   if(timerMode == 1)
   { 
     actualTime = (millis() - startTime) / 1000.0;
     lcd_print(actualTime);
   }
   if(timerMode > 1)
   {
     timerMode = 0;
     timeHistory = actualTime;
     actualTime = 0.0;
   }
}

Well, if we're talking about local vs. variables, I think it's more or less inescapable that having a look at the entire code would be helpful.

Can you elaborate on what it is that your teacher hates about global variables? I myself find them pretty handy; sometimes you can barely do what you need without them. Then again, not all variables need to be global, and usually it's only a small fraction of them.

if you really need the variables' value to be remembered across each loop, then you need variables with a lifetime that is equivalent to what global variables do.

if your teacher does not like global variables, possibly because they are only used in the loop() function, then you need to declare those variables static within the loop() function.

if the variables are used in multiple functions, either you go with global variables, or you pass the variables as parameters to the functions

how big? 1000 lines is relatively small

can you post the global variable declarations? hard to answer the question otherwise

The more important question is whether variables are properly "scoped"; in other words the variable should only be visible to those functions that actually need it.

Problem is not in lines, but i have separated code in 7 files.

I dont know specific reason why he hates global variables. My project is preparation for another students. And they are working in C and now they will be working with arduino. In C they have forbidden to use global variable bcs it is bad habit and it could make some problems, or something like that. Same condition they have in arduino.

In the start of doing project i was using global variables outside of method.

String name= "READY";

then i changed it to static inside a method

String static name= "READY";

What was told to me, that this is "same same, but different, but still same" (meme reference).
So i just wanted to know if i can use eeprom or another way, or it is just stupid overthinkink about easy thing.

it's not a great idea to use EPROM...

May be your teacher would prefer const char* in Flash memory (PROGMEM) rather than a String instance.... but then it's a global storage anyway...

You need to ask the teacher what they mean. Data that are expected to have the same lifetime as the program are going to use up memory anyway....

Ok, thank you for help everybody :slight_smile:

You need to get your teacher to explain that to you, and then get back to us.

it's bad practice if they are shared between files but not between functions within a file.

making them static within a file prevents them from being shared between files

That’s a bit extreme

Serial is a global variable shared across files… that does not make it a bad thing.

It’s all case by case - functions could be generic and take parameters for example instead of working on global variables. That’s a good thing usually.

but that's for the arduino environment, targeted for physical computing.

i doubt any good professional organizations would accept it and understand why teachers might "fail" a student for such use. ( my college professor said he would fail a student for using goto. i eventually understood it's proper use)

What about std::cout, std::wcout - cppreference.com

The global objects std::cout and std::wcout control output to a stream buffer of implementation-defined type (derived from std::streambuf), associated with the standard C output stream stdout.

The usual complain about global variables is that a module (function) then depends upon something outside of its own definition which makes it more difficult to document and understand the function.

Your instructor is probably looking for you to pass any external variable parameters to the function.

For small piece of code it's not a big deal to have globals, but, unless they are well-known and documented in the particular development ecosystem it can make the code hard to understand and maintain.