In C programming language, variables defined within some function are known as Local Variables and variables which are defined outside of function block and are accessible to entire program are known as Global Variables. This article explains the difference between local and global variables. Those variables which are defined within some function and are accessible to that function only are called Local Variables. Those variables which are defined outside of function block and are accessible to entire program are known as Global Variables. Scope is local to that block or function where they are defined. Scope is global i.e. they can be used anywhere in the program. Default value is
unpredictable [garbage]. Default value is Zero [0]. Example: Here x and y are local variables. Example: Here a and b are global variables. Local Vs. Global Variable
#include
void main[]
{
int x=23, y=4;
printf[“x = %d and y=%d”,x,y];
}
#include
int a=10,b;
void main[]
{
printf[“a = %d and b=%d”,a,b];
}
Global variables are those which are not defined inside any function and have a global scope whereas local variables are those which are defined inside a function and its scope is limited to that function only. In other words, we can say that local variables are accessible only inside the function in which it was initialized whereas the global variables are accessible throughout the program and inside every function. Local variables are those which are initialized inside a function and belong only to that particular function. It cannot be accessed anywhere outside the function. Let’s see how to create a local variable.
Example: Creating local variables
Python3
def
f[]:
s
=
"I love Geeksforgeeks"
print
[s]
f[]
Output
I love Geeksforgeeks
If we will try to use this local variable outside the function then let’s see what will happen.
Example:
Python3
def
f[]:
s
=
"I love Geeksforgeeks"
print
[
"Inside Function:"
, s]
f[]
print
[s]
Output:
NameError: name 's' is not defined
Global Variables
These are those which are defined outside any function and which are accessible throughout the program, i.e., inside and outside of every function. Let’s see how to create a global variable.
Example: Defining and accessing global variables
Python3
def
f[]:
print
[
"Inside Function"
, s]
s
=
"I love Geeksforgeeks"
f[]
print
[
"Outside Function"
, s]
Output
Inside Function I love Geeksforgeeks Outside Function I love Geeksforgeeks
The variable s is defined as the global variable and is used both inside the function as well as outside the function.
Note: As there are no locals, the value from the globals will be used but make sure both the local and the global variables should have same name.
Now, what if there is a variable with the same name initialized inside a function as well as globally. Now the question arises, will the local variable will have some effect on the global variable or vice versa, and what will happen if we change the value of a variable inside of the function f[]? Will it affect the globals as well? We test it in the following piece of code:
Python3
def
f[]:
s
=
"Me too."
print
[s]
s
=
"I love Geeksforgeeks"
f[]
print
[s]
Output
Me too. I love Geeksforgeeks
If a variable with the same name is defined inside the scope of function as well then it will print the value given inside the function only and not the global value.
The question is, what if we try to change the value of a global variable inside the function. Let’s see it using the below example.
Example:
Python3
def
f[]:
s
+
=
'GFG'
print
[
"Inside Function"
, s]
s
=
"I love Geeksforgeeks"
f[]
Output:
UnboundLocalError: local variable 's' referenced before assignment
To make the above program work, we need to use the “global” keyword. Let’s see what this global keyword is.
Global Keyword:
We only need to use the global keyword in a function if we want to do assignments or change the global variable. global is not needed for printing and accessing. Python “assumes” that we want a local variable due to the assignment to s inside of f[], so the first statement throws the error message. Any variable which is changed or created inside of a function is local if it hasn’t been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword “global”, as can be seen in the following example:
Example 1: Using global keyword
Python3
def
f[]:
global
s
s
+
=
' GFG'
print
[s]
s
=
"Look for Geeksforgeeks Python Section"
print
[s]
s
=
"Python is great!"
f[]
print
[s]
Output
Python is great! GFG Look for Geeksforgeeks Python Section Look for Geeksforgeeks Python Section
Now there is no ambiguity.
Example 2: Using global and local variables
Python3
a
=
1
def
f[]:
print
[
'Inside f[] : '
, a]
def
g[]:
a
=
2
print
[
'Inside g[] : '
, a]
def
h[]:
global
a
a
=
3
print
[
'Inside h[] : '
, a]
print
[
'global : '
, a]
f[]
print
[
'global : '
, a]
g[]
print
[
'global : '
, a]
h[]
print
[
'global : '
, a]
Output
global : 1 Inside f[] : 1 global : 1 Inside g[] : 2 global : 1 Inside h[] : 3 global : 3
This article is contributed by
Shwetanshu Rohatgi. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.