Function prototyping and nesting of function

Nesting of function

अभी तक हम main() फंक्शन से दूसरे function(फंक्शन) को कॉल करने आए हैं। किंतु हम किसी भी फंक्शन से किसी अन्य फंक्शन को कॉल करवा सकते हैं। यदि एक फंक्शन द्वारा दूसरे फंक्शन को कॉल किया जाता है तो उसे nesting of function(फंक्शन की नेस्टिंग) करते हैं। 

फंक्शन की नेस्टिंग का प्रारूप कुछ इस प्रकार है:- 

main(){
    ------------- - - - - - - 
    - - - - - - - - -  - - - - 
    fun1();
    - - - - - - - - - - - - - - 
     - - - - - - - - - - - - - 
}
fun1(){
     - - - - - - - - - - - - - -
     fun2();
     - - - - - - - - - - - - - 
}
fun2(){
    ------------- - - - - - - 
    - - - - - - - - -  - - - - 
    - - - - - - - - - - - - - - 
     - - - - - - - - - - - - - 
}

Example:-

नीचे 👇 दिए गए Examlple में एक फंक्शन(main के अलावा) दूसरे फंक्शन को कॉल कर रहा है।


    #include <iostream>
    #include <conio.h>
    using namespace std;

    int largest(int m, int n);
    void fun(int x, int y);
    int main()
    {
        int a, b;
        cout << "\n Enter a Number : ";
        cin >> a;
        cout << "\n Enter a Number : ";
        cin >> b;
        fun(a, b);
        getch();
        return 0;
    }

    void fun(int x, int y)
    {
        int z;
        z = largest(x, y);
        cout << "\nlargest Number is " << z;
    }
    int largest(int m, int n)
    {
        int res;
        if (m > n)
            res = m;
        else
            res = n;
        return (res);
    }

Output:-

Enter a Number : 45
Enter a Number : 54
largest Number is 54

Explain Example:-

ऊपर 👆 दिए गए उदाहरण में main() फंक्शन fun(a,b) के माध्यम से fun() नाम के फंक्शन को कॉल कर रहा है। वही जब fun() फंक्शन run होगा तो वह largest(x,y) के माध्यम से largest() नाम के फंक्शन को कॉल कर रहा है।

प्रोग्राम के प्रवाह को निम्न 👇 चित्र द्वारा समझाया गया है। 

Function prototyping  and nesting of function, w3 coding club, w3codingclub

Function prototyping

C language(सी लैंग्वेज) में user-defined function  को यदि प्रोग्राम में declare(डिक्लेअर ) नहीं किया गया हो  तो  तो वह function(फंक्शन)  integer type(इंटीजर टाइप) की वैल्यू को रिटर्न करता है। 

यदि हम फंक्शन द्वारा किसी अन्य टाइप की वैल्यू रिटर्न करवाना चाहते हैं तो उसके लिए हम फंक्शन को पहले प्रोग्राम में डिक्लेअर करना आवश्यक होता है जिसे फंक्शन की prototyping(प्रोटोटाइपिंग) करते हैं, यदि हम चाहते हैं कि फंक्शन कोई भी वैल्यू रिटर्न ना करें तो उस फंक्शन की prototyping(प्रोटोटाइपिंग) की जानी चाहिए।

Sintex of prototyping

main(){
    returan_type fun_name(arg_list);   // prototyping
     _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
    
    var_name = fun_name(arg_list);    //calling
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
    _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
 
{
return_type fun_name(arg_list)  // definition
{
        _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 
        _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
        return(value);
}

Example:-

नीचे 👇 दिए गए उदाहरण में एक फंक्शन द्वारा दो float टाइप की संख्याओं को जोड़कर उसकी वैल्यू को रिटर्न करवाया गया है।


    #include <stdio.h>
    #include <conio.h>
    void main(){
        float a,b,c;
        float add(floatfloat);
        printf("\n Enter a Number : ");
        scanf("%f"&a);
        printf("\n Enter a Number : ");
        scanf("%f"&b);
        c = add(a,b);
        printf("\n The Sum of %f + %f = %f", a,b,c);
        getch();
    } 
    float add(float xfloat y){
        return(x+y);
    }
    

Output:-

Enter a Number : 12.56
Enter a Number : 35.54
The Sum of 12.560000 + 35.540001 = 48.100002

Explain Example:-

ऊपर 👆 दिए गए example में चूंकि add() फंक्शन में फ्लोट वैल्यू भेजी जा रही है, अतः प्रोग्राम में इसका prototype(प्रोटोटाइप ) बताना आवश्यक हो जाता है। float add(float, float) के माध्यम से यह बताया जा रहा है कि यह फंक्शन दो फ्लोट वैल्यू इनपुट में लेगा तथा रिटर्न होने वाली वैल्यू का data type(डाटा-टाइप) भी फ्लोट ही होगा। add() फंक्शन के declaration(डिक्लेरेशन) में return(x + y) के माध्यम से वेरिएबल x तथा y का योग रिटर्न करवाया जा रहा है, जिसे main() फंक्शन में वेरिएबल c में स्टोर कराया गया है।
float add(float, float) में पहले लिखे गए float फ्लोट का अर्थ है कि यह float type की वैल्यू रिटर्न करेगा तथा (float, float) का अर्थ यह है कि यह दो फ्लोट टाइप की वैल्यू को Argument(आरगुमेंट) के रूप में लेगा।

Example:-

नीचे 👇 दिए गए उदाहरण में फंक्शन द्वारा दो float type(फ्लोट टाइप) की संख्याओं का योग ज्ञात कर उन्हें प्रिंट करवाया गया है।

  
         #include <stdio.h>
    #include <conio.h>
    void main(){
        float a,b,c;
        void add(floatfloat);
        printf("\n Enter a Number : ");
        scanf("%f"&a);
        printf("\n Enter a Number : ");
        scanf("%f"&b);
        add(a,b);
        getch();
    } 
    void add(float xfloat y){
        printf("\n The Sum of %f + %f = %f"x,y,x+y);
    }
    

Output:-

Enter a Number : 36.2546
Enter a Number : 45.6589
The Sum of 36.254601 + 45.658901 = 81.913502

Explain Example:-

ऊपर 👆 दिए गए उदाहरण में void add(float, float) के माध्यम से यह बताया जा रहा है कि यह फंक्शन दो फ्लोट वैल्यू इनपुट में लेगा तथा void यह बताता है कि यह फंक्शन कोई भी वैल्यू रिटर्न नहीं करेगा।

Function and array

किसी अन्य वेरिएबल की तरह एरे को भी फंक्शन में आरगुमेंट के रूप में भेजा जा सकता है।  एरे  को फंक्शन में इनपुट के रूप में भेजने के लिए उस एरे  का नाम तथा  एरे  का साइज भेजा जाता है। 

Example:-

नीचे 👇 दिए गए उदाहरण में फंक्शन में एरे  भेजते हुए उसे प्रयोग किया गया है। 


    #include <stdio.h>
    #include <conio.h>
    void array_fun(int *xint y);
    void main(){
      int a[6= {4,5,6,7,8,5};
      array_fun(a,6);
      getch();
    } 

    void array_fun(int *xint y){
        int c; 
        for (c = 0; c < y; c++)
        {
            printf("\n Element %d is : %d", (c+1), x[c]);        
        }        
    }
    

Output:-

Element 1 is : 4
Element 2 is : 5
Element 3 is : 6
Element 4 is : 7
Element 5 is : 8
Element 6 is : 5

Explain Example:-

ऊपर 👆 दिए गए उदाहरण में  array_fun(a,6);   के माध्यम से एरे a  को array_fun() फंक्शन में भेजा जा रहा है। चूंकि एरे का नाम स्वयं में एक pointer(पॉइन्टर) होता है, अतः array declaration(एरे डिक्लेरेशन) में  array_fun(int *x, int y);       में  int *x के माध्यम से pointer value(पॉइन्टर वैल्यू) को स्टोर करवाया जा रहा है। 
चूंकि array_fun() फंक्शन के पास कोई तरीका नहीं होता कि वह यह पता लगा सके कि उसमें कितने element(एलिमेंट) है अतः एरे a के कुल एलिमेंट्स की संख्या 6 भी  फंक्शन में भेजी गई हैं। 


Example:-

ऊपर 👆 दिए गए उदाहरण को इस तरह 👇 से भी किया जा सकता है:-


    #include <stdio.h>
    #include <conio.h>
    void array_fun(int x[]int y);
    void main(){
      int a[6= {4,5,6,7,8,5};
      array_fun(a,6);
      getch();
    } 

    void array_fun(int x[]int y){
        int c; 
        for (c = 0; c < y; c++)
        {
            printf("\n Element %d is : %d", (c+1), x[c]);        
        }        
    }
    

Output:-

Element 1 is : 4
Element 2 is : 5
Element 3 is : 6
Element 4 is : 7
Element 5 is : 8

Explain Example:-

उपरोक्त उदाहरण में   void array_fun(int x[], int y);     में   int x[]    के माध्यम से pointer value(पॉइन्टर वैल्यू) को स्टोर करवाया जा रहा है। function declaration(फंक्शन डिक्लेरेशन) में वेरिएबल के नाम के बाद [] लगा देने से compiler(कंपाइलर)  स्वतः  उस वेरिएबल को pointer variable(पॉइन्टर वेरिएबल) बना देता हैं।  

Post a Comment

0 Comments