In our example programs,
we've declared our variables within the curly braces of the main function.
These variables are called local variables
because their scope or visibility
is limited to the statements in the main function that follow the declaration of the variable.
This is generally what you want to do.
However, sometimes you see variables declared outside of any function,
these variables are called global variables,
and they can be accessed from any part of your program.
Unlike local variables, global variables are automatically initialized to zero.
We'll talk more about scope and visibility as the course progresses.
However, I wanted to introduce you to this concept here
since there is a lot of code out there aimed at beginning c++ programmers
that uses global variables in their examples.
Global variables have their place, but they also come with some potential problems.
Since global variables can be accessed by any part of the program,
this means they can potentially be changed from any part of the program.
So if you've got a program that's thousands of lines of code,
this can make finding errors and debugging more difficult.
Let me show you an example of using global variables in case you see code like this out
there as you study c++.
I'm in the CodeLite IDE.
And you can see that I've declared a variable, called age.
And age is declared right here and it's within the scope of main.
What that means is that this is a local variable.
In other words, it's local to this function.
So I can use age within this function.
So for example here I'm using age
and the compiler is going to say okay which age are we talking about,
and it's going to find the age right here. So if I run this, it'll replace age
with 18.
This is the way we're going to program in this course. However,
you may see code out there that looks a little bit different. Let me show you how that works.
You may see code out there where this is not declared inside of main.
Instead, it's declared outside of main.
This makes age a global variable, which means that it can be accessed from
anywhere in the program.
So now what happens when the compiler sees age here.
First, it looks locally to see if it has a declaration of age.
In this case, it does not.
Then it looks globally to see if age has been declared at the global level.
It has, it finds it right here and now it prints out 18.
Okay. So that's an example of a global variable. It's the concept.
Again it's pretty straightforward
and it's possible to have both global variables and local variables.
So I could do something like this. I could say int age
is 16.
And notice that now I have two variables named age,
I've got one that's local and one that's global.
When the compiler sees the use of age
in the output statement, first, it looks locally.
It looks right here. It finds this age, and it's going to use 16 here,
it will not use this one.
So this one shadows the outer global variable.
That's really all there is to global variables.
Lots of times you'll see code as I mentioned where you won't see this here at all,
and everything is global.
They're okay for teaching purposes, but it's really not a good practice to start using
global variables all over your code.