Passing entire array to a function in C++

Here in this page, we will discuss following two examples,

Here we will discuss two ways to passing array-elements to a function, first by taking input from the user of the array-elements, in the second by declaring the array-elements in the program.

When we pass a entire array in a function, we use the array-name(without subscript operator) in the calling function and array name always refer base address of its first element,

Which means that instead of passing an array-element here, we pass the base address of the first array-element to a function.Where we can access other elements of the array with the help of loop.

 


passing array to a function in single dimensional array

In this method, we pass a entire 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 and then a function has been called outside the loop, which passes this entire array to the function.

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, [] operator will be in the formal parameter

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

Here is an example

#include
#include

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);  //function call with array name

 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 the function get_array(arr). such as,

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

Remember Here, we have only used the name of the array variable arr to pass the array and as the array name always refers to the base address of the 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 if the 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 parameter will be added to the functions,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

The functions are something like this.

 


passing 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 above, such as-

get_array(array-name);

But in Function Definition, [] operator will be in the formal parameter with array-size,

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

Here is the program,

#include
#include

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]; //store element into arr[i]
   }
  }
    get_array(arr);//function call with array name and size

 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 arr to 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 if the 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 the array to a function, So first we store the array-elements in a variable and then all the elements are passed to the function together.


previous-Memory Management and Types

next-String library function and theirs examples