Theoretical question about passing a function variables.

Hey there,

I was working on optimizing a project and something got me wondering. I tried asking google for it, but I'm not sure how to put the problem to words. So I'l give an example.

Here are the two codes that does the same thing in different ways.

Code1

//Pseudo code
String Variable;//Global variable

void SerialReadFunction(){
  Variable = Serial.readline//Assume this return a 64byte string
  doSomeStuff(); //A funtion that does some time consuming operation. Notice we didn't pass anything .

}  

void doSomeStuff(){
  //Do stuff that takes time using GLOBAL "Variable"
}

Code2

//Pseudo code
void SerialReadFunction(){
  String Variable = Serial.readline//Assume this return a 64byte string
  doSomeStuff(Variable); //A funtion that does some time consuming operation using "Variable"

}  

void doSomeStuff(string Variable){
  //Do stuff that takes time using the "Variable" which has been passed to function
}

Now obviously the second code is the proper way of doing things. But does it really matter? What is the difference between using a Global var or a local one, performance wise?

Do global vars take more space in the memory?

I know that using global vars unnecessarily is frowned upon in big projects. But my understanding is that's mostly due to them making the code polluted and not for any performance consideration.

While coding on PC, If I suspect that I might need that variable in the future for something else, I prefer the first variant. I store the variable in an object. That way I can use it from another thread or from another function without adding a reference to the original function or bothering with casts and delegates.

I've been doing that for years on PC. Never had a problem. But since microprocessors we use have significantly lower computational power and RAM compared to a pc, I figured I'd ask gurus for advice and if there is something wrong with them, change my wicked ways.

Global variables actually use fewer micro resources than passing arguments to functions. Twenty years ago, when my micros had 4-8kB of ROM and 128-256 bytes of RAM, almost every variable would be global.

The architectural reasons for not using global variables are wide and varied. Your reason here is as good as any:

illusive:
I know that using global vars unnecessarily is frowned upon in big projects. But my understanding is that's mostly due to them making the code polluted and not for any performance consideration.

Local variables only take less space when the function containing them is not being executed. They are created on the stack when the function is called and do take the same space as the global counterpart.

Your 2nd example will execute a bit slower because placing variables on the stack for passing to the second takes time.

The biggest danger of global variables is that they can unintentionally be changed by every function in your application

sterretje:
Local variables only take less space when the function containing them is not being executed. They are created on the stack when the function is called and do take the same space as the global counterpart.

Your 2nd example will execute a bit slower because placing variables on the stack for passing to the second takes time.

The biggest danger of global variables is that they can unintentionally be changed by every function in your application

I'll be damned. So I was doing it right all those years albeit unintentionally...

So as long as I keep my vars in neatly constructed structs or objects, they take less space AND work faster. That's awesome. Thanks.

Personally I prefer to use local variables where I can.

On a PC speed and memory are hardly an issue.

On a micro you might run into problems. You can not create two 1024 byte global arrays in an Uno. But you can have two 1024 byte local arrays (one in function1 and one in function2) provided function1 does not call function2 (or vice versa).

illusive:
Now obviously the second code is the proper way of doing things.

Both of them are sub-optimal because they use a String (capital s) which can lead to memory corruption in the small memory of an Arduino.

It is much safer to use strings (small s) which are arrays of char terminated with a 0.

...R