Hello, welcome back.
In this lecture, I want to talk about local and global variables.
This is really the concept of function scope or in general program scope.
It has to do with when you can use variables and
when you cannot use them.
We know what functions are.
So now i want to talk about local variables.
Local variables really have to do with more than just functions though.
Local variables have to do with anything that has a block of code.
So variables defined inside of a function are known as
automatic local variables.
So in that function body, in between those starting and ending brackets,
if you declare a variable, that variable is a local variable.
It will automatically be created each time the function is called.
Also, any values in the access to that variable is
only local to the function.
You can't access that variable outside of that function.
So the variable cannot be accessed in other functions if it's
local to a specific function.
It's only accessible from where it's defined.
If an initial value is given to a variable inside of a
function, that initial value is assigned to the variable each
time the function is called.
If you want to retain a value in a variable every subsequent call,
there's actually a way you can do that in c, it's called referred to as a
static variable inside of a function.
That's, again, more of an advanced concept not in a beginner class,
but it is a way that you can actually have different data
every time you invoke functions.
So if you wanted to for example keep a counter of how many times a function
was called, you would declare a local variable inside that function
name static, with a static keyword.
But without just regular local variables are going to
have new values each time.
You can use the auto keyword before the data type, but it's not necessary.
It does make it a little bit more precise.
The compiler is going to add it by default if you don't include it.
But if you do include it, it's just going to make it more
precise, it's not necessary.
So it's called an automatic local variable.
There is a keyword named auto in c, but you don't need to provide it's
provided by default, and that's why you don't see it in a lot of examples
where variables are being defined and declared inside of a function.
Local variables are also applicable to any code where the variable is created
in a block, loops or if statements.
Again, if you have an if statement in what's a block for an if statement,
it's just where the brackets start.
That's referred to as a block.
Blocks can be in functions, blocks can be if statements,
blocks can be in loops.
Blocks can be anywhere that there's brackets, essentially.
Anywhere there's brackets and you create that variable.
Creation of the variable is declaring it and maybe assigning values to it.
But if you declare that variable, you create that variable inside
of a block, you can only use that variable inside the block.
If you try to access that variable outside the block,
it's not going to be used.
So if you have an if statement, inside the if statement you
declare a variable, you can access that variable outside of the if
statement block, same with loops.
It's very important to understand this.
The compiler is going to complain and give you an error if you
try to access it somewhere else.
And what this means is the values in the data is only
applicable to that block.
You're not sharing any data.
Global variables is the opposite of a local variable.
Global variables can be accessed by any function in the program.
The way that you create a global variable is you just put it at
the top outside any function.
So global variables are declared outside of the function and they
do not belong to any function.
Remember, the main keyword the main is a function.
So any variables defined in the main function are only
accessible in the main function.
So the variables in the main function are not accessible
in any other functions.
If you want a variable that's used by all functions, you put the
variable outside of a function, usually at the top of the source
file and usually mark it as saying this is a global variable.
The global variable is a lie for the entirety of the program from
when the program starts to when it stops, that global variable
is going to be located in memory.
Any function in the program can change the value of that global
variable because they have access to it, they can modify it.
And so you should start thinking about wow that seems kind of
dangerous because now you have code that can access the same data
anywhere, but we'll get to that.
If there is a local variable declared in a function with the same name
as a global variable, then that function will use the local variable,
it will mask the global variable.
So if you have a global variable named x and you have a local variable named
x inside of a function, the local variable is going to take precedence.
It's not going to modify the global variables data.
If you assign to that variable, it's only going to
modify the local variable.
So the global variable is not accessible when you have the same
name, and it will actually not be accessed within that local
function, so don't use the same names basically because it's
just going to be confusing.
For people that have programmed a lot, advanced programmers
they're immediately noticed this.
They'll say hey this just acts it's just modifying local
variable even if they see a global verb with the same name.
But for beginners, it's not always very clear.
Let me give you an example.
Here in this example, we're creating a global variable at the
very top before the main function.
And the way you want to organize your program usually have you include
statements at the top after some comments maybe, and then you may have
some defined constants and then you would have your global variables.
Global variables look just like normal variables.
It's just where they're actually declared.
If they're declared outside of a function, then they're global.
So in this example, we have a global variable called my global.
We're initializing it to 0.
We can also initialize these global variables anywhere.
We can initialize them when we declare them or we can
initialize them in any function.
It's an integer.
We also have two functions, the main function in a
function called my function.
Inside there, we have local variables which are only
accessible inside those functions.
In the main function, we have a my local main.
You can only access my local main inside the main function.
But because my global is a variable that's global, you can also
access that in the main function.
So the main can access two variables, the local and the global.
And my function has a local variable named x because it's declared inside
the brackets of the function body and we can actually access the
global variable which is my global.
We can also access x, but we cannot access my local.
My local main I should say.
Because my local main is only inside the main function.
We don't have access to that.
If we try to write or read my local main variable inside the
my function, the compiler is going to give you an error.
You need to understand this concept of local versus global.
Local are inside blocks of code.
When they're declared, you can only use them in the block.
If a variable is outside of a block and it's just in a function or
it's just in a the program itself, you can access it in any function.
Global variables should be avoided.
Global variables are sort of a bad thing.
The reason for them for global variables being bad and why you
should avoid them is because it promotes coupling between functions.
What does coupling mean?
Coupling means dependencies.
If you have a global variable and it's being used by four
functions, that means each one of those functions could be
dependent on the other function.
What's wrong with dependency?
Dependency causes a program to be much harder to debug and find errors.
If you have a bug in your program and you're using a global variable
and the global variable is causing the bug, now that bug could be
anywhere in any function that's using that global variable.
It complicates things when you have global data.
You have a global variable, it could be modified in function one, function
two, function three, function four.
You're not restricting access to that. It's also very hard
once you find the error and you isolate it, it might
be very hard to fix it.
Because now when you fix it, you may cause problems in other functions
because of these dependencies.
Global variables should be avoided as much as possible
because of this coupling, because of these dependencies.
There are times when you may need to use it, but you should avoid it.
And to keep in mind most modern languages in most object
oriented languages like c++ and java, don't even have the
concept of a global variable.
They have static variables which can be class variables, but they're
still encapsulated inside of a class.
Because global data is such a bad thing, new languages
actually try to prohibit.
And you should try to avoid it because it just complicates
your program and makes it harder to find and debug errors.
Instead of using global data, pass data to functions.
If a function needs access to some data, pass it to it.
If -- if it's a lot of data and you don't want to have a lot of parameters,
in theory, you should never really have more than five parameters.
There's a great book out there Called Clean code that talks about the
quality of code and things to avoid things to avoid like global variables.
It also talks about how many parameters you should
have in your functions.
And if a function has 20 parameters, it's an indication that something's
wrong, mainly because that function is probably doing too much.
But a function can have access to data by just passing the data to it and you
can restrict the use of global data.
And for a function that takes a lot of data, you can use something called a
struct which we haven't talked about but that's another way to do that.
So don't use global data as much as possible only when necessary.
Make sure you understand the code.
Make sure you document it really, really well.
So that somebody else looking at the code immediately knows
that you're using global data and knows how everything works.
This is the concept of function scope.
You should need to understand this, so pay attention to all these slides.
And also we'll be using some global data and some local
data in our next challenge. Thank you.