Scope of variables

Scope is a region of a program. Variable Scope is a region in a
program where a variable is declared and used.

Variables are thus of two types depending on the region where these are declared
and used.

Local Variables

Variables that are declared inside a function or a block are called local
variables and are said to have local scope. These local
variables can only be used within the function or block in which these are
declared.

For functions, local variable can either be a variable which is declared in the
body of that function or can be defined as function parameters in the function
definition

Now let's see an example where a local variable is declared in the function
definition.

In this example, variables a and b are declared in the definition of the function multiply and are used within the function. These have no meaning outside the function. 'a' and 'b' are the copies of the variables 'x' and 'y' respectively and store their respective values. Thus, any change in the values of 'a' and 'b' does not affect the values of 'x' and 'y'.
Here, 'a' and 'b' are the local variables for the function 'multiply' and 'x' and 'y' are the local variables for the function main.

Let's take one more example to see that local variables are assets of the function in which these are declared.

In the function func1, we declared a variable x and initialized it with a value 4. This variable is in the body of the function func1 and thus gets destroyed when the body of the function func1 ends. So, when we called func1, 4 gets printed.
We declared another variable x in the function func2 and gave it a value 5. This variable also gets destroyed as the body of func2 ends and has no relation with the variable x of func1.
So, the two variables are independent of each other and are limited to only the function in which these are declared.

Same as function, variables declared inside a block are also local variables and are accessible only within the block.

A block is a group of statements enclosed within the curly braces { }. For example, the body of a function is a block. The body of loops or conditional statements like if..else is also a block.

Global Variables

Variables that are defined outside of all the functions and are accessible throughout the program are global variables and are said to have global scope. Once declared, these can be accessed by any function in the program.

Here, g is a global variable since it is declared outside of the main function. Thus unlike the local variable 'a' which can only be used in the function main, 'g' can be used throughout the program and can be used in all the functions in the program. In this example, we declared 'g' outside of all the functions and gave it a value in the function.

Same as we can change the value of any local variable in a function any number of times and the value last assigned overrides the previous value, we can also override the value of the global variable in a function.
We assigned a value 10 to g at the time of its declaration. Firstly, we called the function func1 in the main function in which we assigned 20 to 'g'. Thus, the value of 'g' became 20 in the function func1 and thus 20 got printed.
After that, we assigned 30 to 'g' in the main function thus making the value of 'g' 30 in the main function and printing 30.

Programming is a skill best acquired by practice and example rather than from books.-Alan Turing