Parameter Passing and Scoping
Parameter Passing and Scoping
#include <iostream>
void swap(int &a, int &b) {
// Simulating copy-in phase
int tempA = a;
int tempB = b;
int main() {
int x = 5;
int y = 10;
std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;
std::cout << "After swap: x = " << x << ", y = " << y << std::endl;
return 0;
}
EXAMPLE:-
procedure doubleIncrement(x)
begin
x := x + 1; // Increment x by 1
print(x); // Print the updated value of x
x := x + 1; // Increment x again by 1
print(x); // Print the final value of x
end;
begin
a := 5;
doubleIncrement(a); // Call the function with argument 'a'
end;
Explanation:
Call by Name :- When doubleIncrement(a) is called, the expression a is
not evaluated immediately. Instead, it is passed as an unevaluated
expression.
Inside the doubleIncrement function :- Each access to x triggers the re-
evaluation of the expression a. This means that a is evaluated twice, once
for each access within the function.
The first print(x) prints the value of a after incrementing it once.
The second print(x) prints the value of a after incrementing it again.
Static scope, also known as lexical scope, is determined during compile-
time based on the program's structure and textual layout. In static scope,
the scope of a variable is defined by its location in the source code
hierarchy. Variables declared within a block, function, or module are
accessible only within that lexical context and any nested scopes within
it. This scoping mechanism facilitates predictability and clarity in code as
the visibility of variables is determined by the program's textual
structure. It enables developers to reason about variable access
statically, without needing to consider the program's execution flow.
Static scoping is widely used in many programming languages, including
C, Java, and Python, where the scope of variables is resolved at compile-
time and remains fixed throughout execution.
Dynamic scoping is a mechanism in programming languages where the
visibility and accessibility of variables are determined by the program's
runtime execution context rather than its textual structure. In dynamic
scoping, the scope of a variable is not fixed by its lexical position in the
source code; instead, it depends on the calling context of the program.
How Dynamic Scoping Works:
1. Runtime Context: In dynamic scoping, the scope of a variable is
determined by the sequence of function calls on the program's call
stack.
2. Variable Lookup: When a function is invoked, the runtime environment
searches for the variable's value starting from the current function's
context and traversing up the call stack until it finds a matching variable
name.
3. Late Binding: Dynamic scoping enables late binding of variables,
meaning that the value of a variable is determined at runtime based on
the calling context rather than at compile-time.
ACCESS TO NON-LOCAL NAMES IN STATIC SCOPE:-
In static scope, access to non-local names, i.e., variables defined outside
the current lexical scope, is determined based on the program's textual
structure. When a variable is referenced within a function or block, the
interpreter or compiler first looks for that variable within the local
scope. If the variable is not found locally, it then searches in enclosing
scopes, moving outward through the lexical hierarchy until it finds a
matching variable name.
Key Points:
1. Lexical Hierarchy: Static scoping follows the lexical hierarchy of the
program's source code. Variables defined in outer scopes are accessible
in inner scopes, but the reverse is not true.
2. Visibility: Non-local names are visible to inner scopes, allowing
functions or blocks to access variables defined in their enclosing
scopes.
3. Shadowing: If a variable with the same name is defined both locally
and in an enclosing scope, the local variable shadows the outer one
within the local scope.
4. Resolution at Compile Time: The resolution of non-local names occurs
at compile-time, based on the program's static structure. This
facilitates predictability and enables early error detection.