Passing Arguments to Functions in C++
In C++, arguments can be passed to functions in two main ways: by value and by reference. Each method has unique characteristics that affect how data is transferred to and used within a function. This article will explain both methods with examples.
1. Passing by Value
When arguments are passed by value, a copy of the argument is created in the function. This means that changes made to the parameter within the function do not affect the original argument outside the function.
Syntax of Passing by Value
return_type function_name(data_type parameter);
Example of Passing by Value
#include <iostream> void incrementByValue(int num) { num = num + 1; std::cout << "Inside incrementByValue, num: " << num << std::endl; } int main() { int number = 10; std::cout << "Before calling incrementByValue, number: " << number << std::endl; incrementByValue(number); std::cout << "After calling incrementByValue, number: " << number << std::endl; return 0; }
In this example:
number
is passed toincrementByValue
by value, so a copy ofnumber
is created within the function.- Modifying
num
inside the function does not affectnumber
outside of it.
2. Passing by Reference
When arguments are passed by reference, the function receives a reference (or alias) to the actual argument. Changes made to the parameter within the function directly modify the original argument.
Syntax of Passing by Reference
return_type function_name(data_type ¶meter);
Example of Passing by Reference
#include <iostream> void incrementByReference(int &num) { num = num + 1; std::cout << "Inside incrementByReference, num: " << num << std::endl; } int main() { int number = 10; std::cout << "Before calling incrementByReference, number: " << number << std::endl; incrementByReference(number); std::cout << "After calling incrementByReference, number: " << number << std::endl; return 0; }
In this example:
number
is passed toincrementByReference
by reference, so no copy is created.- Changes made to
num
inside the function directly modifynumber
outside the function.
3. Comparison: Pass by Value vs Pass by Reference
The main difference between passing by value and passing by reference lies in how arguments are handled:
- Pass by Value: Creates a copy of the argument, so changes made inside the function do not affect the original variable.
- Pass by Reference: Directly refers to the original argument, so any changes made inside the function will affect the original variable.
Example Comparing Pass by Value and Pass by Reference
#include <iostream> void modifyByValue(int a) { a += 10; std::cout << "Inside modifyByValue, a: " << a << std::endl; } void modifyByReference(int &b) { b += 10; std::cout << "Inside modifyByReference, b: " << b << std::endl; } int main() { int x = 5; int y = 5; std::cout << "Before modifyByValue, x: " << x << std::endl; modifyByValue(x); std::cout << "After modifyByValue, x: " << x << std::endl; std::cout << "Before modifyByReference, y: " << y << std::endl; modifyByReference(y); std::cout << "After modifyByReference, y: " << y << std::endl; return 0; }
In this example:
x
is passed by value tomodifyByValue
, so it remains unchanged inmain()
after the function call.y
is passed by reference tomodifyByReference
, so the change inside the function is reflected inmain()
after the function call.
4. Conclusion
Understanding the difference between passing by value and passing by reference is crucial for efficient programming in C++. Use pass-by-value when you do not need to modify the original variable and prefer pass-by-reference when you want the function to affect the original argument.