Parameter Passing_
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
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
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]
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
a=5
b = 10
func(a - b, b) // a - b is passed as an expression
6. Pass-by-Constant (Read-Only)
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?