LOCAL REFERENCING ENVIRONMENTS
Local Variables
- Vars that are defined inside subprograms are called local vars.
- Local vars can be either static or stack dynamic.
- By default an variable in stack dynamic.
Advantages of using stack dynamic variable:
a. Support for recursion.
b. Storage for locals is shared among some subprograms.
Disadvantages of using stack dynamic variable:
a. Allocation/deallocation time required.
b. Indirect addressing “only determined during execution.”
c. Subprograms cannot be history sensitive “can’t retain data values of local vars
between calls.”
Advantages of using static variable:
a. Static local vars can be accessed faster because there is no indirection.
b. No run-time overhead for allocation and deallocation.
c. Allow subprograms to be history sensitive.
Disadvantages of using static variable:
a. Inability to support recursion.
b. Their storage can’t be shared with the local vars of other inactive subprograms.
Example for stack dynamic variable:
int addition(int a, int b) {
int sum = 0;
sum = a+b;
return sum;
}
Example for static variable:
//static variable uses static keyword
int addition(int a, int b) {
static int sum = 0;
sum = a+b;
return sum;
}
Nested Subprograms
- Residing of a subprogram under another subprogram is known as nested subprogram.
- When a subprogram is placed within another subprogram,it gets hide from the rest of the program.
- Algol 68, Pascal, Ada, JavaScript, Python, Ruby allows nesting of subprogram.
- C, do not allow subprogram nesting.
References:
- Sebesta,”Concept of programming Language”, Pearson Edu
- Louden, “Programming Languages: Principles & Practices” , Cengage Learning
- Tucker, “Programming Languages: Principles and paradigms “, Tata McGraw –Hill.
- E Horowitz, “Programming Languages”, 2nd Edition, Addison Wesley