Passing global variables as parameters to a function

I wanted to know what best practice is in the situation where you're going to read or modify global variables in a function - should you pass in those variables as parameters and work with the local variables or simply refer to the global variables in your code without passing parameters?

Yes that one.
If you pass a parameter it creates a new value and the global one will never be updated, unless you pass a pointer to it.

1 Like

The whole point of using a global variable is that it is available anywhere in the global scope so why pass them to a function ?

1 Like

If you want to modify the variable then a direct reference is easier to understand.

Passing global variables as parameters opens up the can of aliasing worms. If in doubt nobody knows which global variable may hide behind a pointer or reference parameter.

1 Like

since you asked, maybe it shouldn't be global

1 Like

Is the following sketch helpful?

byte y = 5;

void setup()
{
  Serial.begin(9600);
  y = add(y);
  Serial.println(y);//shows: 8
}

void loop() {}

byte add(byte z)
{
  return (z + 3);
}

1 Like

It seems pretty pointless, I agree. I'm just concerned that most of my variables seem to be global as they need to persist beyond the loop() function. Is that normal?

What do U mean by a direct reference?

Using the global by its name.

1 Like

It's a general programming best practice to reduce global variable usage to a minimum. But code (especially Arduino code) written by newbies is an accepted exception to that rule.

2 Likes

Ppl use all kinds of variables.

If you meant you wanted variables inside the loop() function to retain their values across the many calls to it that normally happen, you can add static to the declaration of those variables inside the function and they will persist, retain its existence and value even though the function is left and called again.

These would only be available inside the function.

Otherwise, if you use the same variables all over your code, those need to be global.

In a small sketch, global variables are not so bad. As pointed out, as sketches get larger, it becomes a nightmare to keep track of all those variables, so it becomes worthwhile to start exploiting function arguments and local static variables.

a7

2 Likes

And they are almost required for flags which are set and cleared and tested in different functions. OOP makes things a bit easier to encapsulate but still require a shareable handle.

1 Like

Use global variables until you are comfortable with passing arguments (variables) to other functions. The latter allows for modular programming where you can add a feature without disturbing a working program... simply make a function call to the new feature and "pass arguments" to the new feature, as in post #6.

1 Like

Thanks everyone for the helpful tips. In particular I didn't know about static variables.

Just adding that a meaningful application of passing a global variable to a function is if you have a global variable (could be a struct bundling multiple variables) to represent the persistent state of something, e.g. a peripheral device. You may have multiple instances of it because you have multiple devices of the same kind, each represented by a different global variable. The function then takes a pointer/reference to operate on it. Otherwise, you would need to write duplicate functions for each device that you have.
In C++, you could make a class to represent the state with the function as a member function. If the instance of the class is a global variable, the passing of the global variable happens implicitly, but it still happens.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.