0% found this document useful (0 votes)
39 views6 pages

Parameter Passing and Scoping

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views6 pages

Parameter Passing and Scoping

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1.

Parameter Passing :- Parameter passing refers to the process of


providing inputs (parameters) to functions or procedures when they are
called. The parameters enable the function to receive the necessary
data to perform its task. The method of parameter passing can
significantly affect the behavior and efficiency of a program.

2. Formal Parameters :- Formal parameters are the variables defined in


the function declaration or definition that receive the values of the
actual parameters. These parameters act as placeholders for the actual
parameters and are used within the function to refer to the data passed
to it.

3. Actual Parameters :- Actual parameters, also known as arguments, are


the real values or variables passed to a function when it is called. These
are the values that the function uses during its execution. Actual
parameters appear in the function call.

Call by Copy-Restore (Copy-In, Copy-Out)


Call by copy-restore, also known as copy-in, copy-out or value-result, is a
parameter passing method where the value of the actual parameter is
copied into a formal parameter when a function is called, and then
copied back to the actual parameter when the function returns. This
technique combines elements of both pass-by-value and pass-by-
reference.

Here’s how it works in detail:

1. Copy-In (Initialization): When the function is called, the value of the


actual parameter is copied to the formal parameter.
2. Function Execution: The function operates on the formal parameter
as if it were a local variable.
3. Copy-Out (Finalization): When the function execution is complete,
the value of the formal parameter is copied back to the actual
parameter.
This approach ensures that the actual parameter can be modified by
the function, but changes are only visible after the function returns. It
can be seen as a hybrid between call by value and call by reference.

#include <iostream>
void swap(int &a, int &b) {
// Simulating copy-in phase
int tempA = a;
int tempB = b;

// Swapping the temporary variables


int temp = tempA;
tempA = tempB;
tempB = temp;

// Simulating copy-out phase


a = tempA;
b = tempB;
}

int main() {
int x = 5;
int y = 10;

std::cout << "Before swap: x = " << x << ", y = " << y << std::endl;

swap(x, y); // Swap x and y

std::cout << "After swap: x = " << x << ", y = " << y << std::endl;

return 0;
}

Call by Name:- In pass by name mechanism, the name of the procedure


being called is replaced by its actual body. Pass-by-name textually substitutes
the argument expressions in a procedure call for the corresponding
parameters in the body of the procedure so that it can now work on actual
parameters, much like pass-by-reference.

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.

You might also like