array with function in C++ in Hindi

array with function दो अलग अलग concept शामिल हैं,

  • C++ Array
  • C++ Function

यहाँ हम एक-एक करके array -elements को एक function करेंगे जहाँ दिए गए elements user input होंगे।

passing array elements to function में, जैसे ही user, first element enter करता है, वैसे ही function इस element को सीधे function में pass कर देता है। इसके बाद ही second element input किया जाएगा।

Pass Array Element to a function with Single dimensional Array

inside loop , function को cin statement के बाद call किया जाता है अर्थात user के element input करने के बाद function तुरंत call होगा जो इस input किये गए element को pass कर देगा। फिर loop execute होगा और second element input किया जायेगा।

यह क्रम तब तक चलता रहेगा जब तक condition false नहीं हो जाती। इस प्रकार array के सभी elements एक एक करके function में pass होंगे।

जैसा कि आप नीचे syntax में देख सकते हैं,

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

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

  • एक Program में, array element को एक-एक करके pass करने के लिए, function declaration में, हम data type को एक parameter के रूप में declare करते हैं, जैसे-
void get_element(int);
  • जबकि function calling में, एक argument के रूप में एक array variable declare किया जाता है-
get_element(arr[i]);
  • साथ ही function definition में, non -array variable declaration होगा
void get_element(int j)
  {
    ...........;
  }

इसका program नीचे दिया गया है –

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

<strong>void get_element(int);</strong>  // 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]);</strong>  //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

inside loop , जब हम एक array variable arr[i] में एक element store कर रहे होते हैं। तो उसी समय program, element stored होने के बाद, हम उस एक element को function get_element(arr[i]) से भी pass कर रहे हैं जहाँ वो element display हो रहा है। फिर user next element को input करता है।

यहाँ उपरोक्त program में, actual parameter arr[i] की values formal parameter j में copy होंगे, जैसे,

actual parameter = formal parameter

अर्थात,

   arr[i] = j  //[0]=3
   arr[i] = j  //[0]=5
   arr[i] = j  //[0]=2
   arr[i] = j  //[0]=6
   arr[i] = j  //[0]=1

Pass Array-Element to function with multi dimensional Array

जैसा कि आप नीचे दिए गए syntax में देख सकते हैं जो ऊपर जैसा ही है,

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++?

void get_element(int);  // function declaration

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

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

इसका program यहाँ दिया गया है-

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

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

दोनों program ऊपर दिया गए program की तरह ही execute होगा।

Related exercise:

Leave a Reply

Your email address will not be published.