pass elements to a function one by one

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

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

When we pass the array elements one by one to 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-elements to a function of single dimensional array

as you can see in below syntex, 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 one by one to a function?

In a program, to pass an array element one by one, in function declaration, we declare a non-variable as argument, like-

void get_element(int);

Whereas in the calling function, as 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
#include

void get_element(int);  // declare a non - array function

void main()
{
  clrscr();
  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]);  //pass element one by one into function
  }
 getch();
}

// 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]);
}

Meaning that as soon as an array-element is being stored, we are also passing that element through a function void get_element(int j)  before user entered second element. 

void get_element(int j)
{
   cout<<"From function: "<<j;
}

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-elements to a function of two dimensional array

as you can see in below syntex 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.

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

How to pass element one by one to a function in two dimensional-array?

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
#include

void get_element(int);       // declare a non - array function

void main()
{
  clrscr();
  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
   }
  }
 getch();
}

// 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,


previous-Memory Management and Types

next-String library function and theirs examples