C++ Function Parameters
return_type function_name(type param) { // pass by value
// body
}
return_type function_name(type ¶m) { // pass by reference
// body
}
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 std::cout and std::cin
#include <iostream>
void increment(int x) {
x = x + 1;
}
// Program execution starts in main()
int main() {
// Declare num and set it to 5
int num = 5;
increment(num);
// Print to the console with cout
std::cout << num << std::endl;
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
void update(int &x) {
x = x + 1;
}
// Program execution starts in main()
int main() {
// Declare num and set it to 5
int num = 5;
update(num);
// Print to the console with cout
std::cout << num << std::endl;
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
// Include std::string
#include <string>
void print(const std::string &name) {
// Print to the console with cout
std::cout << "Hello, " << name << std::endl;
}
// Program execution starts in main()
int main() {
print("Alex");
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
int addThree(int a, int b, int c) {
// Send a + b + c back to the caller
return a + b + c;
}
// Program execution starts in main()
int main() {
// Print to the console with cout
std::cout << addThree(1, 2, 3) << std::endl;
// Return 0 to signal that the program finished successfully
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 std::cout and std::cin
#include <iostream>
void doubleValues(int arr[], int size) {
// Loop: repeat while the condition holds
for (int i = 0; i < size; i++) {
arr[i] *= 2;
}
}
// Program execution starts in main()
int main() {
int nums[] = {1, 2, 3};
doubleValues(nums, 3);
// Loop: repeat while the condition holds
for (int i = 0; i < 3; i++) {
// Print to the console with cout
std::cout << nums[i] << " ";
}
// Print to the console with cout
std::cout << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- Expecting a function to change the caller's variable when it received a copy by value.
- Passing an array and using
sizeofon the parameter, which gives the size of a pointer rather than the array. - Forgetting
conston a reference parameter, so the function can change the argument accidentally.
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