← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 6 of 15

C++ Arrays & Functions

Passing an array to a function hands over the original row of boxes, so the function can read or change them. You usually also pass the size, because the function does not know it.
Syntax
cpp
return_type function_name(data_type array_param[], int size) {
  // body
}

function_name(array_name, size);

Passing Arrays to Functions

Passing an array to a function uses the array's name without brackets as the argument, while the receiving parameter is written with empty brackets, like void process(int arr[]).

Under the hood, what's actually passed is the array's starting memory address, not a full copy of its contents.

Example: Passing Arrays to Functions

cpp
// Include std::cout and std::cin
#include <iostream>

void printArr(int arr[], int size) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < size; i++) {
		// Print to the console with cout
		std::cout << arr[i] << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
}

// Program execution starts in main()
int main() {
	int nums[] = {1, 2, 3};
	printArr(nums, 3);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Passing Array Size

Because an array parameter is really just a pointer to its first element, the function has no way to know how many elements the original array actually contains — you have to pass the size explicitly as a separate integer parameter, like void process(int arr[], int size), or the function risks reading past the array's real bounds.

Example: Passing Array Size

cpp
#include <iostream>

void printArr(int arr[], int size) {
	for (int i = 0; i < size; i++) {
		std::cout << arr[i] << " ";
	}
	std::cout << std::endl;
}

int main() {
	int nums[] = {4, 5, 6, 7};
	printArr(nums, 4); // size must be passed separately
	return 0;
}

Modifying Arrays in Functions

Since arrays are effectively passed by reference (via their memory address), any modification a function makes to the array's elements directly changes the original array back in the caller — unlike ordinary variables, there's no automatic protective copy being made.

Example: Modifying Arrays in Functions

cpp
// Include std::cout and std::cin
#include <iostream>

void doubleAll(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};
	doubleAll(nums, 3);
	// Print to the console with cout
	std::cout << nums[0] << " " << nums[1] << " " << nums[2] << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Passing Multi-dimensional Arrays

When passing a 2D array to a function, the first dimension's size can be omitted from the parameter, like void process(int grid[][4]), but the second (column) dimension must always be specified exactly, since the compiler needs it to calculate the memory offset for each row.

Example: Passing Multi-dimensional Arrays

cpp
// Include std::cout and std::cin
#include <iostream>

void process(int grid[][4], int rows) {
	// Loop: repeat while the condition holds
	for (int i = 0; i < rows; i++) {
		// Loop: repeat while the condition holds
		for (int j = 0; j < 4; j++) {
			// Print to the console with cout
			std::cout << grid[i][j] << " ";
		}
	}
	// Print to the console with cout
	std::cout << std::endl;
}

// Program execution starts in main()
int main() {
	int grid[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
	process(grid, 2);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Const Array Parameters

Adding const to an array parameter, like void print(const int arr[], int size), signals that the function will only read the array's contents and promises not to modify them — the compiler will then flag any accidental write inside the function as an error, catching a class of bugs before the program even runs.

Example: Const Array Parameters

cpp
#include <iostream>

void print(const int arr[], int size) {
	for (int i = 0; i < size; i++) {
		std::cout << arr[i] << " ";
		// arr[i] = 0; // compile error: cannot modify a const array parameter
	}
	std::cout << std::endl;
}

int main() {
	int nums[] = {1, 2, 3};
	print(nums, 3);
	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 sizeof inside the function to give the array length.
  2. Forgetting to pass the array size, so the function reads past the end.
  3. Assuming arrays are copied when passed, when the function actually works on the original.

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.