Function Parameters
Parameters let you pass data into a function. They act like local variables initialized with the values you provide when calling the function.
void display(int count) {
for (int i = 0; i < count; i++) {
cout << i << " ";
}
}
Pass by Value vs Reference
By default, C++ passes arguments by value — the function gets a copy. Changes inside the function don't affect the original.
void doubleVal(int x) {
x = x * 2;
}
int main() {
int num = 5;
doubleVal(num);
cout << num;
return 0;
}
Use a reference parameter (&) to modify the original variable directly.
void doubleRef(int &x) {
x = x * 2;
}
Use references when you want to avoid copying large data or need to modify the argument.
Try it Yourself →