In C++, an array is a collection of elements that are stored in a contiguous memory location and contain the same data type . In C++, we can pass the array to functions either by pointer or by reference. Arrays are mainly passed to functions to perform several operations on array elements efficiently, which helps to maintain the modular code.
In C++ , if we want to reuse the array logic, we can create a function. We need to provide only the array name to pass an array to function in C++.
Syntax
It has the following syntax:
Return_type function_name(data_ype array_name); //passing array to function
C++ Passing Array to Function Example: print array elements
Let's see an example of a C++ function that prints the array elements.
Example
#include <iostream>
using namespace std; //using standard namespace
void printArray(int arr[5]);
int main() //main function
{
int arr1[5] = { 10, 20, 30, 40, 50 };
int arr2[5] = { 5, 15, 25, 35, 45 };
printArray(arr1); //passing array to function
printArray(arr2);
}
void printArray(int arr[5])
{
cout << "Printing array elements:"<< endl;
for (int i = 0; i < 5; i++)
{
cout<<arr[i]<<"\n";
}
}
Output:
Printing array elements:
10
20
30
40
50
Printing array elements:
5
15
25
35
45
Methods to Passing an Array to Function in C++
There are several methods that we can use to pass an array to function in C++. Some main methods are as follows:
- Passing Array as a Pointer
- Passing array as a Reference
- Passing as a Sized Array
- Passing as an Unsized Array
Passing Array as a Pointer
In C++, passing an array as a pointer is the most common and efficient method for passing an array to a function. In this method, we can pass the memory address of the initial array element that allows the function to handle arrays of the dynamic size.
Syntax
It has the following syntax:
returntype functionname (datatype *array_name)
In this syntax,
- functionname: It represents the name of the function.
- datatype: It represents the type of data.
- *array_name: It represents the pointer array.
C++ Passing Array as a Pointer Example
Let us take an instance to demonstrate how we can pass an array as a pointer in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
void show(int *arr, int size) { // Function to print elements of the array
cout << "Array elements are: ";
for (int i = 0; i < size; ++i)
cout << arr[i] << " ";
cout << endl;
}
int main() { //main function
int num[] = {5, 10, 15, 20, 25, 30};
int size = sizeof(num) / sizeof(num[0]);
// Passing array as a pointer
show(num, size);
return 0;
}
Output:
Array elements are: 5 10 15 20 25 30
Explanation:
In this example, we have taken the int *arr in the display function that receives the base address of the numbers array. After that, the array decays into a pointer, so the arr[i] function accesses elements via pointer arithmetic. Finally, we also pass the size to indicate the number of elements.
Passing Array as a Reference
In C++, passing an array as a reference to a function is an important method that is mainly used to hold the size of the array and avoid the pointer decay. It represents the function that gets a reference to the original array. It enables the function to access and modify the actual elements of the array and hold its size details at compile time.
Syntax
It has the following syntax:
returntype functionname(datatype (&arr_name)[size])
C++ Passing an Array as a Reference to Function Example
Let us take an example to illustrate we can pass an array as a reference to a function in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
// Function taking array as reference
void print_array(int (&arr)[5]) {
cout << "Array elements are: ";
for (int i = 0; i < 5; ++i)
cout << arr[i] << " ";
cout << endl;
}
int main() { //main function
int number[5] = {12, 25, 16, 30, 34};
// Pass array by reference
print_array(number);
return 0;
}
Output:
Array elements are: 12 25 16 30 34
Explanation:
In this example, we have taken the print_array function, an array of 5 integers by reference, which ensures the retained array size. After that, it shows every array element using the for loop. Finally, this method prevents pointer decay and enables us to direct access to the original array.
Passing as a Sized Array to Function
In C++, passing as a sized array to a function is a common method to pass an array to a function. If we want to pass an array to a function, we can specify its size in the function's parameter list. It is known as passing a sized array. However, the array decays to a pointer when passed, which specifies the size that helps in accessing the correct number of elements.
Syntax
It has the following syntax:
returntype FunctionName (datatype arrName [size], int size)
C++ Passing as a Sized Array to Function Example
Let us take an instance to demonstrate how we can pass as a sized array to function in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
// Function taking array as a sized array
void show(int arr[5]) {
cout << "Elements in the array: ";
for (int i = 0; i < 5; ++i)
cout << arr[i] << " ";
cout << endl;
}
int main() { //main function
int nums[5] = {4, 8, 12, 20, 25};
show(nums); // Passing array
return 0;
}
Output:
Elements in the array: 4 8 12 20 25
Explanation
In this example, we have taken the show function that takes a fixed-size array of 5 integers, but the main function defines an array nums with 6 elements. In the show function, the loop tries to access 6 elements, which leads to undefined behavior because arr[5] is outside the declared size of the parameter array.
Passing as an Unsized Array
In C++, passing as an unsized array to a function is similar to passing as a sized array. We can pass an unsized array to a function by avoiding the size in the parameter declaration.
Syntax
It has the following syntax:
return_type function_name (data_type array_name[])
C++ Passing as an Unsized Array to Function Example
Let us take an instance to demonstrate how we can pass as an unsized array to function in C++.
Example
#include <iostream>
using namespace std; //using standard namespace
// Function taking unsized array
void show(int arr[], int size) {
cout << "Unsized Array elements are: ";
for (int i = 0; i < size; ++i)
cout << arr[i] << " ";
cout << endl;
}
int main() { //main function
int nums[] = {7, 14, 21, 28, 35};
int len = sizeof(nums) / sizeof(nums[0]);
show(nums, len); // Pass array and its size
return 0;
}
Output:
Unsized Array elements are: 7 14 21 28 35
Explanation:
In this example, we have taken the show function that accepts an unsized array (int arr) and its size as parameters. After that, the array nums is passed with its length, which allows the function to iterate through and display each element.
C++ Example to Pass Multidimensional Array to a Function
Let us take an instance to demonstrate how we can pass a multidimensional array to a function in C++.
Example
#include <iostream>
using namespace std; // using standard namespace
void printMatrix(int arr[3][2]) //function using 2D array with 3 row and 2 columns
{
cout << "Elements of Matrix are:" << endl;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 2; ++j) {
cout << arr[i][j] << " ";
}
cout << endl;
}
}
int main() { // main function
int matrix[3][2] = {
{9, 8},
{7, 6},
{5, 4}
};
// Passing 2D array to function
printMatrix(matrix);
return 0;
}
Output:
Elements of Matrix are:
9 8
7 6
5 4
Explanation:
In this example, we have taken a 3×2 two-dimensional array that is passed to the printMatrix function. After that, the function takes a fixed-size 2D array and shows all its elements via nested loops.
C++ Passing an Array to Function FAQs
1) Can we pass an array to a function in C++?
Yes, we can pass an array to functions via pointers or via references in C++.
2) Can we change array elements within a function in C++?
Yes, we can change the array elements within a function but it can affect the original array.
3) Can we use the sizeof function inside a function to find the size of the array in C++?
No, the sizeof function only works in the calling function.
4) How can we pass an array using the reference in C++?
In C++, we can pass an array using the reference via the following syntax:
void func(int (&arr)[5])
5) What are the several methods to pass an array to a function in C++?
There are several methods to pass an array to a function in C++, such as pass a pointer, pass as a reference, pass as a sized array, and pass as an unsized array.