pass array elements to function in C++

array with function in C++ has included two concepts are,

here we will discuss with the following two examples,


In both cases, we will pass array-elements one by one to a function where given elements will take from the user.

When we pass the array elements to a the function, as soon as the user enters the first element, the function passes this entered element directly to the function, then the second element will be entered by the user.

Pass Array Element to a function with Single dimensional Array

as you can see in below syntax, inside the loop, the function is immediately called after the cin statement. Meaning, once an element is entered, the function will immediately pass that one element.

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

How To Pass Element to a Function with Single dimensional array ?

  • In a program, to pass an array element one by one, in the function declaration, we declare a non-variable as an argument, like-
void get_element(int);
  • Whereas in the calling function, as an argument is declared an array-variable such as
get_element(arr[i]);
  • Also in Function Definition, there will also be a non Array variable declaration such as
void get_element(int j)
  {
    ...........;
  }

Here is an example,

#include<iostream>
using namespace std;
void get_element(int); // declare a non - array function

int main()
{
  int arr[5]; //define array size

  cout<<"Enter the five Element in Array\n";

  for(int i=0;i<5;i++)
  {
    cout<<"arr["<<i<<"]=";
    cin>>arr[i];   //store element one by one into arr[i]

    get_element(arr[i]);</strong>  //pass element one by one into function
  }
return 0;
}

// function definition
 void get_element(int j)  //catch element one by one using normal variable
  { 
    cout<<"From function j="<<j<<"  \n"; // print these element using normal variable
  }

OUTPUT

Enter the five Element in Array
arr[0]=3
From function j=3  
arr[1]=5
From function j=5  
arr[2]=2
From function j=2  
arr[3]=6
From function j=6 
arr[4]=1
From function j=1

Explanation

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

arr[5] = 3 5 2 6 1

Inside the loop, when we are storing an element in an array-variable arr[i]. At the same time, after having an element store, we are also passing that one element to the function get_element(arr[i]).

Here, you can see.

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

Here in the above program, value of the actual parameter arr[i] will be copied in the formal parameter j, such as,

arr[i] = j 
   arr[i] = j
   arr[i] = j
   arr[i] = j 
   arr[i] = j

Pass Array-Element to function with multi dimensional Array

as you can see in below syntax which is same as above, inside the loop, the function is immediately called after the cin statement. Meaning, once an element is entered, the function will immediately pass that one element before enter second element by user.

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

How to Pass Element to a function in multi dimensional-array in C++?

here is the simple step are

void get_element(int);  // function declaration

  get_element(arr[i][j]);    //function calling

  void get_element(int element)  // function definition 
  {
      ...........;
  }

Here is the program,

#include<iostream>
using namespace std;
void get_element(int);       // declare a non - array function

int main()
{
  int arr[2][3];      //define array size

  cout<<"Enter the five Element in 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 from user

    get_element(arr[i][j]);    //pass element one by one to function
   }
  }
return 0;
}

// function definition
 void get_element(int element)  //catch element one by one using normal variable
 {
   cout<<"From Function: "<<element<<endl; // print these element using normal variable
 }

OUTPUT

Enter the five Element in Array
arr[0][0] = 3
From Function: 3 
 
arr[0][1] = 2
From Function: 2 
 
arr[0][2] = 1
From Function: 1 
 
arr[1][0] = 5
From Function: 5 
 
arr[1][1] = 4
From Function: 4 
 
arr[1][2] = 6
From Function: 6

Both program will be execute in same way,

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] = 3 2 1 5 4 6

Inside the loop, when we are storing an element in an array-variable arr[i][j]. At the same time, after having an element store, we are also passing that one element to the function get_element(arr[i][j]).

Here, you can see.

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

after this, function void get_element(int element) will  be display  the element one by one.

more about function with Array,

passing Entire array to a function with Single dimensional Array and multi-dimensional Arrays ?

previous-Memory Management and Types

next-String library function and Their examples

Like it?

Leave a Reply

Your email address will not be published.