Local Referencing Environment in Subprograms
Local Referencing Environment in Subprograms
1. Local Variables:
x=5
y=3
z = add(x, y) # The local referencing environment is created when 'add' is called
print(z)
○ The local variables a, b, and result are all part of the local
referencing environment of the add function.
○ These variables are used only within the add function and cannot be
accessed outside of it.
3. When add finishes:
○ Once the return value is computed, the function terminates, and the
activation record is popped from the stack.
○ The local variables a, b, and result are destroyed.
○ The calling program (outside add) receives the result from add via
the return statement.
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
Each of these recursive calls has its own local referencing environment, with
separate instances of n and separate memory allocations for each activation record.
When the recursion starts unwinding, each function call returns and its activation
record is popped from the stack.