Hello everyone, my name is Atharva and we are continuing with the C programming course.
In the previous video we had discussed function call parameters, in which the first way was call by value method and the second was call by reference method.
Now after knowing all these things, now we are going to talk about what is a the scope of a variable.
You might find this topic a little different because we have already learned a lot about the variables in so many videos, but here we are talking about scope of a variable.
Now, we will say what is the scope? Scope is sounding something new? Let’s understand what exactly is the meaning of scope here.
So, scope is the part of the program where a defined variable can have its existence and beyond that it cannot exist.
So, this basically is the definition of the scope.
Now, we will understand it in layman terms, what is it trying to say? Any such part of the program in which we have defined a variable, that variable will be valid in that particular part itself, if you want to use the variable before or after the programme, before or after that particular part.
We won't be able to access it because it won't be existing over there.
To understand this thing, we will go ahead and see how these things are of so much importance in the program.
We're going to go ahead and see how the scope can be used to define different things.
So, the scope basically or this boundary within which the variable exists and it doesn't exist out of it.
So, this boundary is defined in three ways, in which the first scope is of the local variables.
Now, what is this local variable? local variables like we used to use if statements or for loops statements or even in functions, the curly brackets that we used to use.
So, these curly brackets are basically the scope for the local variables.
So, whenever we will make any variable inside these curly brackets, we will initialise them.
At that time what will happen is that the variable will be valid only inside of those curly brackets.
If we want to print the value of that particular variable outside of those curly brackets or we wish to store any value in that.
Our compiler will give us an error because that particular variable was local to that curly bracket, which means it was local to it and it was existing within it.
After that we will see local variables.
Now what is the local variable? Any such variable which is not inside any curly brackets.
It would be out of all curly brackets.
And it is on the top of the main function.
We can call it this way, these are such variables, which we cannot define in any function.
We define it outside of the function.
And we call it local variables.
What happens with this? Those variables can be used in any function, anywhere, under any scope.
Third, we will talk about our formal parameters.
We had already learned that in our function definition the parameters list that we pass, those parameters are called the formal parameters because their existence is limited inside the function definition, we cannot access it out of it.
So, we have discussed all these three scopes.
Now, we will see an example of the local variables.
Now, what is this example doing? One simple X, Y, Z, three variables we have made and it is adding X and Y and storing it Z and it is printing all three values.
So, this is a simple program.
Now, it is worth seeing that X, Y and Z, the three variables that we have made.
These will basically be called as local variables.
Why is thar? Because you can see that these are made or written inside this particular curly bracket.
So, what will happen? If outside of the main function, somewhere here, if I wish to use these X, Y, Z variables.
I will not be able to use them.
Why? Because these are local to this particular area only and they will be restrained to this particular area itself.
They will be available and will exist in this restricted area only.
Outside of it we can make a variable of the same name but it will not possess this particular value.
We will have to put a new value inside it, that is the reason why we call this local variable because it is local to these curly brackets.
Which means it is local to this main function.
Now we will talk about our global variables, which is the second type of scope that we saw.
How is the global variable? It is the same program, only the Z variable in this, we had made inside the Z function.
That we have written basically outside the main function, on the top.
So, here we have defined Z.
That is the reason we call it a global variable.
You can see that it is not in any function, it is not in any function definition, neither is it in the main function.
So, it is right outside of everything.
So, when you're using Z in the main function.
Our compiler will not show an error here.
As he knows that you have declared a global variable named Z.
So, inside the main function, you don't have to define it again as long as you don’t want it.
So, this is a way to define a variable locally or globally.
Apart from this the third type that we saw of formal parameters, this is what we know very well.
In this the function definition that is there, in that whichever is the parameter’s list, in that basically the variables that we are adding we call those as formal parameters.
And you can see that this A and B, these are local to this particular function only and inside it it's validity will remain.
If you print a and b inside this function, it will give you the same value which is there in this particular function.
But if you wish to print its value outside of the function, it will not print.
In this way, these are our formal parameters, local variables and global variables.
These we call the scope of a variable.
Which means one particular variable in which area will stay limited to and it will exist in and outside of it, it won't exist.
Who tells us this? This is told to us by the scope of the variable.
After reading all these scopes now we have completed our function’s part.
So, we have seen the functions in the C program.
If you think that there is any part of this video or this topic, you might have not understood then you can ask us any query or question related to that topic without any inhibition.
You just have to go on forums.learnvern.com, you have to type your query there in your own language and we will come up with the solution or the answer as soon as possible in your own language.
Apart from this if you wish to have a discussion on any topic.