Answer in Brief
Explain the types of scopes for variable or LEGB rule with example.
Types of Scope:
There are four types of Scope namely
Local Scope, Global Scope, Enclosed Scope, and Built-in Scope.
- Local scope refers to variables defined in the current function.
- Always, a function will the first lookup for a variable name in its local scope.
- Only if it does not find it there, the outer scopes are checked.
- On execution of the above code, the variable a displays the value 7, because it is defined and available in the local scope.
- A variable that is declared outside of all the functions in a program is known as a global variable.
- This means the global variables can be accessed inside or outside of all the functions in a program.
- On execution of the above code the variable ‘a’ which is defined inside the function displays the value 7 for the function call Disp() and then it displays 10; because a is defined in the global scope.
- All programming languages permit functions to be nested. A function (method) within another function is called a nested function.
- A variable that is declared inside a function that contains another function definition within it, the inner function can also access the variable of the outer function. This scope is called the enclosed scope.
- When a compiler or interpreter search for a variable in a program, it first search Local and then search Enclosing scopes.
- In the above example Displ() is defined within Disp().
- The variable ‘a’ defined in Disp( ) can be even used by Displ( ) because it is also a member of Disp().
- The built-in scope has all the names that are pre-loaded into the program scope when we start the compiler or interpreter.
- Any variable or module which is defined in the library functions of a programming
language has a Built-in or module scope. They are loaded as soon as the library files are imported to the program.
- The LEGB rule is used to decide the order in which the scopes are to be searched for scope resolution.
- The scopes are listed below in terms of hierarchy (highest to lowest).
|Local (L)||Defined inside function/ class|
|Enclosed(E)||Defined inside enclosing functions (Nested function concept)|
|Global (G)||Defined at the uppermost level|
|Built-in(B)||Reserved names in built-in functions (modules)|
The scope also defines the order in which variables have to be mapped to the object in order to obtain the value.
1. x:= ‘outer x variable’
2. display ():
3. x:= ‘inner x variable’
4. print x
- When the above statements have executed the statement (4) and (5) display the result as
outer x variable
inner x variable
- The above statements give different outputs because the same variable name ‘x’ resides in different scopes, one inside the function display() and the other in the upper level.
- The value ‘outer x variable’ is printed when x is referenced outside the function definition.
- Whereas when display() gets executed, ‘inner x variable’ is printed which is the x value inside the function definition.
- From the above example, we can guess that there is a rule followed, in order to decide from which scope a variable has to be picked.
Concept: Types of Variable Scope
Is there an error in this question or solution?