- Another aspect of user defined functions
is the scope of a variable.
We will specifically distinguish
between local and global variables.
What is the scope?
The scope is the area in which a
variable is known to the code.
In other words, global variables are
recognized throughout the entire code,
the entire program, so, they span an area
that's identical with every single function
and every single piece of code in that program.
That's why they're called global.
Local variables are recognized only
within the containing function.
You could declare two variables by
the same name in two different functions
and although they have the same name,
they would be two different variables
that will only be recognized
by their respective owning functions.
Finally, if a local variable has
the same name as the global one,
the local variable supersedes the global one.
So, the closer you are to the use of the variable,
the more precedence it takes in terms
of conflict resolution if the names are identical.
Let's take a look at the code and see how
the things will affect how we code.
In this exercise, we are going to look
at several global variables which may
or may not have been assigned a value in the main
or inside one or more functions.
We are going to track the changes in
the values of those variables
to further explore issue of scope
and make sure that we all understand
the implication of naming, changing values
in a function, outside a function.
Let's examine first the two functions we have in this code.
They're very simple.
One changes a number, the other one
is changing it differently.
The first function, changeNumber has one parameter, a.
Inside the function, a has been assigned the value 5.
The second function, changeNumberDifferently,
it's also an int a, however, it sets values
to either variables b and c.
Where are those b and c coming from
since they're not defined in the function?
Well, notice that a is the parameter, b is coming from here,
so, in fact, a, b, and c are all global variables.
So, in function changeNumber we have a variable a,
and the question is:
Is a the same as the parameter of the function
or it is the same as the global variable a?
In this case, we follow the rule
that the closer to where the variable
is being used supersedes the other definition.
So, in this case, int a is an internal variable
known to that function, therefore in line 37,
a refers to the parameter a.
b has not been defined in this function,
therefore there's no conflict,
therefore b is the global variable, so is c.
However, that's only at first look.
Notice the int c, which means there's
a new declaration of c, which means
that this particular c is not the same
as the global variable c.
So, we have an a that's not the same as the global a
because it is a parameter.
We have a c that's not the same as the global c
because it's defined inside a function
and only b is the one that's left.
So, I'm going to run this program
and then follow line by line in the code
and then see what happened.
First of all, in main we have a = 0,
because it has been initialized a declaration.
Then we call changeNumber a.
changeNumber a, assign the value 5 to a,
so, inside changeNumber a now becomes 5,
and we are printing the value again
as soon as changeNumber finished.
So, obviously since a = 5 it's
an internal statement of the function
and a is the parameter known only to the function.
Even though they called a, it's not the same a.
So, outside the function, the a is still zero.
Now, we're going to call the function
changeNumberDifferently with the same a which is zero.
So, in main, it's a zero, and now inside the function,
we assigning a = -1, b = 3, c = -1.
We're running the function, let's see what happened.
a is still zero because it hasn't
been changed by a function.
The only thing those function did
is they made the change internally to their parameter.
b is now equal with 3 because it is a true
global variable unaffected by anything else,
and c has also been unaffected outside
because the variable c inside changeNumber,
it's a local variable.
So, we had multiple instances variables
with the same name not being changed
and being very confusing.
The main lesson from here is to first of all
name your variable in a very meaningful manner
so that you avoid any kind of confusion
by looking at output, seeing variables
with same name with multiple values,
and second, be very careful what you define,
and where and what values you assign.