Course Content
C++ Introduction
0/1
C++ Variables & Constants
0/1
C++Scope of Variable
0/1
C++ Keywords & Identifiers
0/1
C++ Data Types
0/1
C++ Basic I/O
0/1
C++ Type Conversion
0/1
C++ Operators
0/1
C++ Comments
0/1
C++ If-else
0/1
C++ Ternary Operator
0/1
C++ for Loop
0/1
C++ Ranged for Loop
0/1
C++ while/do-while Loop
0/1
C++ break Statement
0/1
C++ Continue Statement
0/1
C++ switch Statement
0/1
C++ goto Statement
0/1
C++ Functions
0/1
C++ User-defined Functions
0/1
C++ Default Arguments
0/1
C++ Storage Class
0/1
C++ Recursion
0/1
C++ Return by Reference
0/1
C++ Arrays
0/1
C++ Multi-dimentional Arrays
0/1
C++ Arrays & Function
0/1
C++ String
0/1
C++ Structure
0/1
C++ Structure & Functions
0/1
C++ Pointers to Structure
0/1
C++ Pointers
0/1
C++ Void Pointers
0/1
C++ Pointers & Arrays
0/1
C++ Pointers & Functions
0/1
C++ Dynamic Memory Allocation
0/1
C++ OOPs Concepts
0/1
C++ Objects and Class
0/1
C++ Constructors
0/1
C++ Destructors
0/1
C++ Constructor Overloading
0/1
C++ Objects & Function
0/1
C++ Enumeration
0/1
C++ Inheritance
0/1
C++ Inheritance Access Control
0/1
C++ Inheritance Types
0/1
C++ Polymorphism
0/1
C++ Function Overloading
0/1
C++ Function Overriding
0/1
C++ Operator Overloading
0/1
C++ Friend Function
0/1
C++ Virtual Function
0/1
C++ Abstract Class & Pure Virtual Function
0/1
C++ Encapsulation
0/1
C++ Abstraction
0/1
C++ Templates
0/1
C++ Exception Handling
0/1
C++ Multithreading
0/1
C++ Standard Library
0/1
C++ Programming Tutorials
About Lesson

C++ Passing Array to a Function

In this tutorial, we will learn how to pass a single-dimensional and multidimensional array as a function parameter in C++ with the help of examples.


Passing Array to a Function

  • we can pass arrays as an argument to a function just like we pass variables as argument. And, also we can return arrays from a function.
  • In order to pass array to the function we just need to mention the array name during function call.
  • Before you learn about passing arrays as a function argument, make sure you know about C++ Arrays and C++ Functions.

Syntax

The syntax for passing an array to a function is:

returnType functionName(dataType arrayName[arraySize]) {
// statement
}

Let’s see an example,

int total(int num[2]) {
// statement
}

Here, we have passed an int type array named num to the function total(). The size of the array is 2.


Declaration Function with array as a Parameter

There are two possible ways to do so, one by using call by value and other by using call by reference.

    1. We can either have an array as a parameter.
int sum(int arr[]);
    1. Or, we can have a pointer in the parameter list, to hold the base address of our array.
int sum(int* ptr);

we will study the second way in details later when we will study pointers.


Passing array as parameter to Function

Now let’s see a few examples where we will pass a single array element as argument to a function, a one dimensional array to a function and a multidimensional array to a funcion.

Example 1: Passing single Array to a Function

Let’s write a very simple program, where we will declare and define an array of intergers in our main() function and pass one of the array element to a function, which will just print th evalue of the element.

// C++ Program to display single array element
#include <iostream>
using namespace std;
void arr(int a ) {
    // display array elements
        cout << "Numbers " << a <<endl;
}

int main() {
    // declare and initialize an array
    int myArr[] = {1, 2, 3};
    arr(myArr[1]); // pass array element myArr[1] only.

    return 0;
}

Output

Numbers : 2

Example 2: Passing 1D Array to a Function

// C++ Program to display numbers of 4 students
#include <iostream>
using namespace std;

// declare function to display Roll Number & take a 1d array as parameter
void display(int a[4]) {
    cout << "Display Roll Numbers: " << endl;

    // display array elements
    for (int i = 0; i < 4; ++i) {
        cout << "Student " << i + 1 << ": " << a[i] << endl;
    }
}
int main() {
    // declare and initialize an array
    int rollNum[4] = {4, 1, 3, 2};
    
    display(rollNum);    // call display function pass array as argument

    return 0;
}

Output

Display Roll Numbers: 
Student 1: 4
Student 2: 1
Student 3: 3
Student 4: 2

Working of above program

  1. When we call a function by passing an array as the argument, only the name of the array is used.
    display(rollNum);

    Here, the argument rollNum represent the memory address of the first element of array rollNum[4].

  2. However, notice the parameter of the display() function.
    void display(int a[4])

    Here, we use the full declaration of the array in the function parameter, including the square braces [].

  3. The function parameter int a[4] converts to int* a;. This points to the same address pointed by the array rollNum. This means that when we manipulate a[4] in the function body, we are actually manipulating the original array rollNum.

    C++ handles passing an array to a function in this way to save memory and time.


Passing Multidimensional Array to a Function

We can also pass Multidimensional arrays as an argument to the function. For example,

Example 3: Program to Store value entered by user in 3D array

// C++ Program to display the elements of 2D array by passing it to a function
#include <iostream>
using namespace std;

// define a function & pass a 2d array as a parameter
void display(int a[][2]) {
    cout << "The Elements are: " << endl;
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            cout << "arr[" << i << "][" << j << "]: " << a[i][j] << endl;
            }
        }
    }
int main() {

// initialize 2d array
int arr[2][2] = { {1, 2}, {3, 4} };
    // call the function & pass a 2d array as an argument
    display(arr);

    return 0;
}

Output

The Elements are: 
arr[0][0]: 1
arr[0][1]: 2
arr[1][0]: 3
arr[1][1]: 4

Working of above program

  1. In the above program, we have defined a function named display(). The function takes a two dimensional array, int a[][2] as its argument and prints the elements of the array.
  2. While calling the function, we only pass the name of the two dimensional array as the function argument display(arr).
  3. Note: It is not mandatory to specify the number of rows in the array. However, the number of columns should always be specified. This is why we have used int a[][2].

We can also pass arrays with more than 2 dimensions as a function argument.


C++ Returning an Array From a Function

  • We don’t return an array from functions, rather we return a pointer holding the base address of the array to be returned.
  • But we must, make sure that the array exists after the function ends i.e. the array is not local to the function.
  • We can also return an array from the function. However, the actual array is not returned.
  • Instead the address of the first element of the array is returned with the help of pointers.

We will learn about returning arrays from a function in the later tutorials.

Exercise Files
No Attachment Found
No Attachment Found
error: Content is protected !!