Open In App

Pass Array to Functions in C++

Last Updated : 03 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, a collection of elements stored in contiguous memory locations and having the same data type is called an array. Passing arrays to functions is done to perform various operations on array elements without messing up with the main code.

In C++, an array can be passed in a function using a pointer or reference. Understanding the different approaches to pass arrays is important for writing code according to the needs.

Methods to Pass Array to a Function in C++

In C++, we have the following ways to pass an array as a parameter to the function:

  1. As a sized array
  2. As an unsized array
  3. As a pointer (pass by pointer)
  4. As a reference (pass by reference)

1. Passing as a Sized Array

In this method, we pass the array in the same way we declare it with the array type, name, and size. As we can see, we still have to pass the size of the array as another parameter because at the end, the array will be treated as a pointer in the function.

Syntax

return_type function_name (datatype array_name [size], int size)

Example

The below example demonstrates the passing of array as sized array.

C++




// C++ program to demonstrate the passing of array as sized
// array.
  
#include <iostream>
using namespace std;
  
// function to update array elements
void printarray(int a[10])
{
    for (int i = 0; i < 5; i++)
        a[i] = a[i] + 5;
}
  
int main()
{
    // array declaration
    int a[5] = { 1, 2, 3, 4, 5 };
    printarray(a); // Passing array to function
  
    // printing array elements
    for (int i = 0; i < 5; i++)
        cout << a[i] << " ";
    return 0;
}


Output

6 7 8 9 10 

2. Passing as an Unsized Array

This method is similar to the previous method, but the difference is that we dont specify the size of the array.

Syntax

return_type function_name (data_type array_name[])

Example

The below example demonstrates the passing of array as unsized array.

C++




// C++ program to demonstrate the passing of array as
// unsized array.
  
#include <iostream>
using namespace std;
  
// function to update array elements
void printarray(int a[],int size)
{
    for (int i = 0; i < size; i++)
        a[i] = a[i] + 5;
}
  
int main()
{
    // array creation
    int a[5] = { 1, 2, 3, 4, 5 };
    int n=5;
    printarray(a,n); // Passing array to function
  
    // printing array elements
    for (int i = 0; i < n; i++)
        cout << a[i] << " ";
    return 0;
}


Output

6 7 8 9 10 

3. Passing Array as a Pointer

In this method, we pass the memory address of the first element of the array. This method also allows for dynamic array sizes.

Syntax

return_type function_name (datatype *array_name)

Example

The below example demonstrates how to pass array as a pointer to function.

C++




// C++ program to demonstratethe passing of array by
// pointer.
  
#include <iostream>
using namespace std;
  
// function to print array elements
void printarray(int* a)
{
    for (int i = 0; i < 5; i++)
        *(a + i) = *(a + i) + 5;
}
  
int main()
{
    // array creation
    int a[5] = { 1, 2, 3, 4, 5 };
    printarray(a); // Passing array to function
    for (int i = 0; i < 5; i++)
        cout << a[i] << " ";
    return 0;
}


Output

6 7 8 9 10 

4. Passing Array as a Reference

In this method, we pass an array to a function as a reference and also explicitly pass its size i.e. we are passing both the reference to the first element and the size of the array.

Syntax

return_type function_name(data_type (&arr)[size])

Example

C++




// C++ to demonstrate array passing by pass by reference
#include <iostream>
using namespace std;
  
void modifyArray(int (&arr)[5])
{
  // deducing size
  int size = sizeof(arr) / sizeof(int);
    for (int i = 0; i < size; ++i) {
        arr[i] *= 2;
    }
}
  
int main()
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int n = 5;
    modifyArray(arr);
    for (int i = 0; i < n; i++) {
        cout << arr[i] << " ";
    }
  
    return 0;
}


Output

2 4 6 8 10 

FAQs on Passing Array to Function in C++

Q1. What is the difference between pass by pointer and pass by reference for array?

Answer:

In pass by pointer, we pass the address of the first element. In pass by reference we need to pass both the reference to the first element and, if specified, the size of the array.

Also, in pass by pointer method, array decay occurs, but not in pass by reference method.

Q2. Can we modify the original array inside a function?

Answer:

Yes, if modifications are made to the array inside a function then it will affect the original array as the arrays can only be passed by pointer or reference.

Q3. Can passing of an array to a function be done using std::array(in C++17 or later)?

Answer:

Yes, using std::array you can pass array with size information as the std::array is a container instead of plain old datatype Arrays.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads