C और C++ प्रोग्रामिंग में फंक्शन्स

फंक्शन (Function) in C++

C++ में C language के सभी inbuilt Function का प्रयोग किया जा सकता हैं तथा User अपने अनुसार भी फंक्शन (Function) का निर्माण कर सकता हैं जिन्‍हें user defined फंक्शन (Function) कहा जाता हैं। C++ का Program Structured Programming के अंतर्गत होती है, अर्थात् इसमें Program को विभिन्‍न फंक्शन (Function) में विभाजित कर बनाया जाता हैं। फंक्शन (Function) का program में महत्‍वपूर्ण योगदान होता हैं। फंक्शन (Function) के द्वारा कई कार्य आसानी से किये जा सकते हैं।

फंक्शन (Function) एक प्रकार का Program होता हैं जो एक विशेष कार्य को करता हैं। फंक्शन Program को Short करने के अलावा Program की Execution Speed भी बढ़ाता हैं।

फंक्शन (Function) Prototype

फंक्शन Prototype, Compiler को फंक्शन (Function) के Interface के सम्‍बन्‍ध में अर्थात् फंक्शन (Function) में दिये जाने वाले Argument की संख्‍या Argument के data type एवं Return value type से संबंधित जानकारी प्रदान करती हैं।

फंक्शन Prototype का निर्धारण Function declaration के समय किया जाता हैं। जब फंक्शन को call करते हैं तब Compiler declaration के समय किये गये prototype से फंक्शन की तुलना करता हैं अर्थात् वह यह सुनिश्चित करता हैं कि फंक्शन calling time पर सही-सही Argument दिये गये हैं तथा Return type सही हैं या नहीं।

Function Prototype एक declaration Statement होता हैं जिसका Syntax निम्‍नलिखित हैं।

Syntax:-

return_type function_name (argument_list);


यहाँ

  • return_type :- फंक्शन के द्वारा Return किये जाने वाले data का प्रकार हैं।
  • function_name :- फंक्शन को दिया जाने वाला नाम हैं।
  • argument_list :- फंक्शन को दी जाने वाली Argument की संख्‍या और उनका प्रकार होता हैं।

Example :-

  • int area (int, float);
  • Float add (float a, float b);
  • void input (void);
  • void output ( );
  • void input ( );

तथा

  • int sum (int a, int b, int c);

इसे निम्‍न प्रकार से लिख सकते हैं –

  • int sum (int, int, int)

फंक्शन declaration के समय Compiler Argument के Type को निश्‍चित करता है, उन के नाम को नही, अत: जब हमें फंक्शन को केवल declare करना होता हैं। तो उसे Argument type अकेले देकर भी declare किया जा सकता हैं।

Creating Function

C++ में फंक्शन (Function) को विभिन्‍न प्रकार से बनाया जा सकता है। Function prototype के आधार पर मुख्‍यत: फंक्शन चार प्रकार के होते हैं –


  1. With argument with return
  2. With argument but no return
  3. No argument but return
  4. No Argument but no return

इन सभी फंक्शन में से हम पहले वाले को समझ लेते हैं बाकीं फंक्शन भी आप नीचे दिए हुए उदहारण के आधार पर बना सकते हैं| सामान्तः सिर्फ पहला और दूसरा फंक्शन का ज्यादा प्रयोग किया जाता है|

Function with argument with return

जब हम किसी फंक्शन में argument देते हैं और फंक्शन उन argument पर Process करके Result को Return करता हैं। तो ऐसे फंक्शन (Function) को With Argument With Return Function कहा जाता हैं। यदि हमें दो संख्‍याओं को जोड़ने का program बनाना हैं तो हम इस हेतु फंक्शन (Function) निम्‍न प्रकार से बनायेंगे।

#include<iostream.h>
#include<conio.h>
int add (int, int) ; //Function declaration
void main ()
{
clrscr ();
int x, y;
cout<<“\n enter any two value”;
cin>>x>>y;
int r;
r = add (x, y); //फंक्शन (Function) calling
cout <<“total =”<<r;
getch ();
}

int add (int a, int b); // Function definition
{
int c;
c = a+b;
return(c);
}

अथवा निम्‍न प्रकार बना सकते हैं –

int add (int a, int b); // Function declaration
{
int c;
c = a+b;
return c;
}

void main()
{
clrscr();
int x = 13;
int y = 14;
cout<<add (x,y);
}


error: Content is protected !!