← Back to C++ Course | Chapter 5: Functions | Lesson 2 of 13

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

cpp
#include <iostream>

void increment(int x) {
	x = x + 1;
}

int main() {
	int num = 5;
	increment(num);
	std::cout << num << std::endl;
	return 0;
}

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

cpp
#include <iostream>

void update(int &x) {
	x = x + 1;
}

int main() {
	int num = 5;
	update(num);
	std::cout << num << std::endl;
	return 0;
}

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

cpp
#include <iostream>
#include <string>

void print(const std::string &name) {
	std::cout << "Hello, " << name << std::endl;
}

int main() {
	print("Alex");
	return 0;
}

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

cpp
#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;
}

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

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.