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

C++ Function Parameters

Parameters are the empty slots in a function you fill in each time, like the blanks in a recipe. You can pass a copy of a value or the original itself.
Syntax
cpp
return_type function_name(type param) {         // pass by value
  // body
}

return_type function_name(type &param) {        // 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

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

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

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

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

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 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Expecting a function to change the caller's variable when it received a copy by value.
  2. Passing an array and using sizeof on the parameter, which gives the size of a pointer rather than the array.
  3. Forgetting const on a reference parameter, so the function can change the argument accidentally.

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.