Pass entire Array to a Function in C++ ?

Pass entire array to a function in C++ are possible ? here we will discuss this with following,

You can’t pass an entire to a function although you can pass the address of array’s first element using by array-name(without subscript operator) in the actual parameter. 

don’t confuse with Passing Array Elements to a Function and Passing Array to a Function, Both are quite different.

In the passing array elements to function value will be passed and function will be call multiple time until array element ends while passing array to function only first element’ address will be passed and inside the function other elements will be access using loop means function has single call.

let’s explore it,

And we already mentioned in Array in C++ that array name always refer the base address of its first element, so you can say we only pass the base address of its first element rather than all elements address to a function where inside function body we use the loop to access all other elements just like in normal array.


Pass Array to a Function in Single Dimensional Array

In this method, we pass an array to a function. see below syntex-

for(int i=0;i<size;i++) 
  { 
    cin>>arr[i]; 
  }
 get_element(arr);

In the syntax given above, inside the loop, all the elements of the array are stored in array variable arr[i] and then a function has been called outside the loop, which will be passed base address of its first element.

How to pass array into a function?

Firstly in function declaration, as arguments declare an array,

void get_array(int[]);

whereas in calling function, the function can be called only by array name ( means, without [] operator), such as-

get_array(array-name);

But in Function Definition, arr[] name will be declared with subscript operator []

void get_array(int arr[])
  {
     ..........;
  }

Here is an example

#include<iostream.h>
#include<conio.h>

void get_element(int[]);// function declaration

void main()
{
  clrscr();
  int arr[5];

  cout<<"Enter the five Element in the Array\n";
  for(int i=0;i<5;i++)
  {
    cout<<" arr["<<i<<"]=";
    cin>>arr[i]; //store element into arr[i]
  }
    get_element(arr);  //passing first element address

 getch();
}

// function definition
void get_element(int arr[])
{
   cout<<"Element display using function";
   for(int i=0;i<5;i++)
   {
    cout<<"\n arr["<<i<<"]="<<arr[i];
   }
}
OUTPUT:-
Enter the Element in the Array
arr[0]=3
arr[1]=5
arr[2]=2
arr[3]=4
arr[4]=8

Element display using function
arr[0]=3
arr[1]=5
arr[2]=2
arr[3]=4
arr[4]=8

explanation

In the program, input is taken from the user which will be stored in the array-variable arr[].such as,

arr[5] = 3 5 4 2 8

First, inside the loop, we have stored the elements from the user in the array-variable arr[i]. Then out of the loop, this array is passed to function void get_array(arr). such as,

 for(.....)
   {
      ....
      cin>>arr[i];
   }
 get_array(arr);

Remember, here we only arr name to pass the base address of its first element into function get_array(arr)

Inside the function body we use loop to access other elements.

But in Single Dimensional array-elements are already declare in the program

Then,

void main()
{
  clrscr();
  int arr[5]={3,4,2,1,4};
  get_element(arr);

 getch();
}

and other statements will be same as above. 

Note here, if the array size is given by the user, then only one extra parameter will be added to the function,then

void get_array(int[],int); // in function declaration 

void get_array(arr,size); // in function calling

void get_array(int arr[],int size){ } // in function definition

Pass Array to a Function in Two Dimensional Array

Its syntax is slightly different from the syntax of the above program,

Firstly in function declaration, as arguments declare an array,

void get_array(int[2][3]);

whereas in calling function, the function can be called only by array name ( means, without [] operator) same as in single dimensional array, such as-

get_array(array-name);

But in Function Definition, But in the formal parameter arr will be declared with its array-size,

void get_array(int arr[2][3])
 {
    ..........;
 }

Here is the program,

#include<iostream.h>
#include<conio.h>

void get_array(int[2][3]);// function declaration
void main()
{
  clrscr();
  cout<<"\nEnter Five Element in the Array\n";
 for(int i=0;i<2;i++)
  {
    for(int j=0; j<3;j++)
     {
       cout<<"arr["<<i<<"]["<<j<<"] = "; 
       cin>>arr[i][j];
     }
  }
    get_array(arr);//base address will be passed

 getch();
}

// function definition
void get_array(int arr[2][3])
{
  cout<<"\nElement display using function\n";
  for(int i=0;i<2;i++)
  {
    for(int j=0; j<3;j++)
    {
      cout<<"arr["<<i<<"]["<<j<<"] = "<<arr[i][j]<<endl;
    }
  }
}
OUTPUT:-

Enter six Element in the Array
arr[0][0] = 2
arr[0][1] = 1
arr[0][2] = 4
arr[1][0] = 5
arr[1][1] = 7
arr[1][2] = 6

Element display using function
arr[0][0] = 2
arr[0][1] = 1
arr[0][2] = 4
arr[1][0] = 5
arr[1][1] = 7
arr[1][2] = 6

Explanation

In the program, input is taken from the user which will be stored in the array-variable arr[i][j].such as,

arr[2][3] = 2 1 4 5 7 6

First, inside the loop, we have stored the elements from the user in the array-variable arr[i][j]. Then outside of the loop, the entire array is passed to the function get_array(arr). such as,

 for(.....)
   {
     ....
     cin>>arr[i][j];
   }
 get_array(arr);

Remember Here, we have only used the name of the array-variable arrto pass the array elements and as we know, array name always refers to the base address of its first element, so we can say that here we pass the base address of the first element to the function void get_array(arr[][]).

Where in function void get_array(arr[][] we use loop to access other elements of array.


But in two dimensional array Elements are already Declare in the program

Then,

void main()
 {
   clrscr();

   int arr[2][3]={
 		  {3,4,3},
 		  {3,2,4}
 		 };
     get_array(arr);

   getch();
 }

and other statements will be same as before. 

when we pass an array to a function then only base address will be passed of array’s first element while Passing Array Elements to a function C++ we pass value not address it is also the main difference between them.


previous-Memory Management and Types

next-String library function and theirs examples