0% found this document useful (0 votes)
6 views5 pages

Parameter Passing_

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)
6 views5 pages

Parameter Passing_

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/ 5

Parameter Passing:

parameter passing methods refer to the strategies used to pass data (arguments)
between functions or procedures. Each method has its rules about how the values
of arguments are passed and how they can be modified by the called function.
Below are the main parameter-passing methods with their descriptions, advantages,
and examples.

Implementation Models:

• Pass-by-value

• Pass-by-result

• Pass-by-value-result

• Pass-by-reference

• Pass-by-name

1. Pass-by-Value

● Description: A copy of the actual parameter (argument) is passed to the


function. Changes made to the parameter inside the function do not affect
the original argument.
● Key Characteristics:
○ Only the value is passed.
○ The original data remains unchanged.
○ Memory is used to create a copy of the data.

Example (C/C++/Java):
void func(int x) {
x = x + 10; // Modifies only the local copy
}

int main() {
int a = 5;
func(a);
printf("%d", a); // Output: 5
}

2. Pass-by-Reference

● Description: The address or reference of the actual parameter is passed to


the function. Changes made to the parameter inside the function directly
modify the original argument.
● Key Characteristics:
○ No copying of data; only references are passed.
○ Modifies the original data.
○ Efficient for large data structures.

Example (C++/Python):
void func(int &x) {
x = x + 10; // Modifies the original variable
}

int main() {
int a = 5;
func(a);
cout << a; // Output: 15
}
def func(x):
x.append(10) # Modifies the original list

a = [1, 2, 3]
func(a)
print(a) # Output: [1, 2, 3, 10]

3. Pass-by-Value-Result (Copy-In, Copy-Out)

● Description: A copy of the actual parameter is passed to the function (like


pass-by-value), and any changes made to the parameter inside the function
are copied back to the original argument when the function ends.
● Key Characteristics:
○ Both input and output behavior.
○ Used in languages like Ada.
○ Expensive due to copying.

Example (Pseudocode):
Procedure func(x)
x = x + 10 // Local copy modified
End

a=5
func(a)
print(a) // Output: 15 (value copied back to 'a')

4. Pass-by-Result (Copy-Out)

● Description: The function does not get an initial value for the parameter, but
it writes the final value to the actual parameter at the end of the function
execution.
● Key Characteristics:
○ Only output behavior.
○ Original value is ignored.

Example (Pseudocode):
Procedure func(x)
x = 10 // Final value assigned
End

a=5
func(a)
print(a) // Output: 10

5. Pass-by-Name

● Description: The actual parameter is not evaluated before being passed.


Instead, the parameter is substituted into the function as an expression (lazy
evaluation). This allows arguments to be evaluated only when used.
● Key Characteristics:
○ Expressions are re-evaluated every time they are accessed.
○ Used in languages like Algol.
Example (Pseudocode):
Procedure func(x, y)
if x > 0 then y = y + 1 // y evaluated when x > 0
End

a=5
b = 10
func(a - b, b) // a - b is passed as an expression

6. Pass-by-Constant (Read-Only)

● Description: The actual parameter is passed as a constant value. The


function cannot modify the original argument.
● Key Characteristics:
○ Provides safety against unintended changes.
○ Common in languages like C++ (using const).

Example (C++):
void func(const int &x) {
// x = x + 10; // Error: cannot modify
cout << x;
}

int main() {
int a = 5;
func(a);
}

Summary Table:
Parameter Passing Original Data Copy Key Use Case
Method Modified? Created?

Pass-by-Value No Yes Simple, safe

Pass-by-Reference Yes No Efficient for large


data
Pass-by-Value-Result Yes Yes (in and Bi-directional data
out) flow

Pass-by-Result Yes No (only out) Output-only


parameters

Pass-by-Name Depends No Lazy evaluation

Pass-by-Constant No No Read-only access

These methods provide flexibility for handling parameters based on requirements


like efficiency, safety, and readability in programming languages.

You might also like