Local constants--when to use?

So I know that, in general, constants should be declared globally. But I just had an example(password-guessing game) program where the password was declared as a local constant. So now I'm not sure when to use local, and when to use global. I know it's not a huge deal, but I'd like to be clear.
TIA
closed account (Lv0f92yv)
It depends, in part, on where you plan/need to access these variables. Global variables are available to your program throughout its entirety (someone correct me if this is wrong), hence have 'global scope'. Local variables are kind of like temporary variables, which are 'erased' when the function in which they are declared returns or finishes executing and is popped off the stack.

If you only want/need to use a variable inside one given function, declare it locally (inside that function). If you need a variable to be accessed everywhere, declare it global. These are by no means the absolute rules all the time.

Pointers are another powerful tool provided in c and c++, which allow you to pass a variable by reference to functions that can change their value - regardless of scope.
I'm anything but an expert, but I had always been told the converse, use globals sparingly. I think that applies more to variables than to constants, but the same principal is the same. Unless you have a constant that needs to be accessed by pretty much the whole program, define your constants locally. Suppose you need 1 constant, say the number 10, to be the max size of an array, but latter on you need another constant, say the number 20, to be the max iteration of some for loop. If you declared the first constant as MAX; you cannot declare the second constant as MAX; also, if you do you will either have a syntax error (or correct me if I'm wrong), if you declare it within a scope resolution operator such as {} the second constant will override the first constant and you will no longer have access to the first constant within that scope. Defining variables and constants locally allows the user to use logical names such as MAX without having to resort MAX1, MAX2, MAX3 etc which is very helpful in larger programs with many variables and constants.

That being said, I personally declare many constants and variables globally and have had relatively few problems, but I am a beginner and only write smaller programs. I've even gotten a couple of classes to have access to 'universal' variables... I don't remember how... but a RNG class I wrote once was able to access variables not defined in the class.... I certainly wouldn't ever reccomend that though because its a pain to debug. so if your working as part of a school group or project best to stick with the mandated style and if the teacher says global constants, use global constants.
On these forums, most of the people have been told that globals are evil. I don't agree with them, however if you're trying to make modular code, then those globals might not be as good of an idea. Nevertheless...

Desh is right.

-Albatross
On these forums, most of the people have been told that globals are evil.


To be fair, I wasn't really told this, I just discovered it for myself. ;P Or if I was told, it didn't sink in until I saw for myself.


Anyway here's my 2 cents:

Globals are a quick-n-dirty way to get something done. They admittedly make it easier to slap something together in a rush, but they have numerous other problems that beginners tend to not notice. These problems become more and more apparent the larger a project becomes, or the more time that passes between project updates.

Constants are no exception. They suffer from all the same ails that global variables do.


The biggest problem with globals is that they destroy expandability / code reusability (Albatross mentioned this somewhat... "if you're trying to make modular code"). Basically... code you write that uses globals will only work with those globals. That is, they'll only work with that single data set.

Let's take this classic example. Say you're making a game and you want to have a routine that heals the player:

1
2
3
4
5
6
7
8
// playerlife, playermaxlife are both global

void HealPlayer(int heal)
{
  playerlife += heal;
  if(playerlife > playermaxlife)
    playerlife = playermaxlife;
}


Seems harmless... but what if you decide later that you want multiple players? Do you have to write a seperate routine for each one?

What if you want to recycle this routine so it can heal enemies as well? Do you create a seperate HealEnemy function that is just a copy/paste but uses different variables?

Globals make any code that uses them inflexible. The code cannot be applied to similar tasks where it probably should be able to.
Topic archived. No new replies allowed.