Question:

What is meant by scope of a variable? Briefly explain Local and Global scope.

Show Hint

Local variables exist inside functions, while global variables are accessible throughout the program.
Updated On: Mar 14, 2026
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

Step 1: Define variable scope.
The scope of a variable refers to the region of a program where the variable can be accessed or used.
Scope determines the visibility and lifetime of variables in a program.
Step 2: Explain Local scope.
A local variable is declared inside a function and can be accessed only within that function.
Once the function finishes execution, the local variable is destroyed.
Example:
\[ \texttt{def show():} \] \[ \texttt{\ \ x = 5} \] \[ \texttt{\ \ print(x)} \] Here \(x\) is a local variable.
Step 3: Explain Global scope.
A global variable is declared outside all functions and can be accessed throughout the program.
Example:
\[ \texttt{x = 10} \] \[ \texttt{def show():} \] \[ \texttt{\ \ print(x)} \] Here \(x\) is a global variable.
Step 4: Conclusion.
Thus the scope of a variable determines where it can be used, and variables may have local or global scope.
Was this answer helpful?
0
0