C++ Function Parameters
Pass by Value
Passing by value gives the function its own independent copy of whatever argument you provide, so any changes made to the parameter inside the function have zero effect on the original variable back in the caller. This is the safest default, since the caller's data is fully protected from unintended modification.
Example: Pass by Value
#include <iostream>
void increment(int x) {
x = x + 1;
}
int main() {
int num = 5;
increment(num);
std::cout << num << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Pass by Reference
Passing by reference, using & in the parameter type like void update(int &x), gives the function direct access to the original variable itself rather than a copy — any change made inside the function is immediately visible to the caller too. This is essential whenever a function needs to actually modify the caller's data, such as swapping two values.
Example: Pass by Reference
#include <iostream>
void update(int &x) {
x = x + 1;
}
int main() {
int num = 5;
update(num);
std::cout << num << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Const Parameters
Combining const with a reference parameter, like void print(const string &name), gets you the performance benefit of reference passing (no copy is made, which matters for large objects like strings) while still guaranteeing the function can't accidentally modify the caller's data. This pattern is extremely common in real C++ code for exactly that reason.
Example: Const Parameters
#include <iostream>
#include <string>
void print(const std::string &name) {
std::cout << "Hello, " << name << std::endl;
}
int main() {
print("Alex");
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Multiple Parameters
A function can accept as many parameters as it needs, listed in order and separated by commas in both its definition and every call site — the arguments you pass must match the parameter list's order and count (or be convertible to the expected types).
Example: Multiple Parameters
#include <iostream>
int addThree(int a, int b, int c) {
return a + b + c;
}
int main() {
std::cout << addThree(1, 2, 3) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Array Parameters
Arrays are passed to functions by reference by default in C++, even without an explicit &, which means any changes a function makes to array elements do affect the original array back in the caller. This is different from ordinary variables and is a common surprise for beginners expecting array parameters to behave like value parameters.
Example: Array Parameters
#include <iostream>
void doubleValues(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] *= 2;
}
}
int main() {
int nums[] = {1, 2, 3};
doubleValues(nums, 3);
for (int i = 0; i < 3; i++) {
std::cout << nums[i] << " ";
}
std::cout << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions