Advertisements
Advertisements
प्रश्न
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:
- 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.
Example:
- On execution of the above code, the variable a displays the value 7, because it is defined and available in the local scope.
Global 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.
Example:
- 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.
Enclosed 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.
Example:
- 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().
Built-in Scope:
- 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.
LEGB rule:
- 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.
Example:
1. x:= ‘outer x variable’
2. display ():
3. x:= ‘inner x variable’
4. print x
5. display()
- When the above statements have executed the statement (4) and (5) display the result as
Output:
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.
shaalaa.com
Types of Variable Scope
या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?