C ++ फ़ंक्शन (उदाहरणों के साथ)

इस ट्यूटोरियल में, हम उदाहरणों की मदद से C ++ फंक्शन और फंक्शन एक्सप्रेशंस के बारे में जानेंगे।

एक फ़ंक्शन कोड का एक ब्लॉक है जो एक विशिष्ट कार्य करता है।

मान लीजिए हमें एक सर्कल बनाने और इसे रंग देने के लिए एक कार्यक्रम बनाने की आवश्यकता है। हम इस समस्या को हल करने के लिए दो कार्य बना सकते हैं:

  • एक समारोह सर्कल आकर्षित करने के लिए
  • सर्कल को रंग देने के लिए एक फ़ंक्शन

एक छोटी सी समस्या को जटिल रूप से विभाजित करना हमारे कार्यक्रम को समझने और पुन: प्रयोज्य करने में आसान बनाता है।

समारोह के दो प्रकार हैं:

  1. मानक लाइब्रेरी फ़ंक्शंस: C ++ में पूर्वनिर्धारित
  2. उपयोगकर्ता द्वारा परिभाषित फ़ंक्शन: उपयोगकर्ताओं द्वारा बनाया गया

इस ट्यूटोरियल में, हम ज्यादातर उपयोगकर्ता-परिभाषित कार्यों पर ध्यान केंद्रित करेंगे।

C ++ उपयोगकर्ता-परिभाषित फ़ंक्शन

सी ++ प्रोग्रामर को अपने स्वयं के फ़ंक्शन को परिभाषित करने की अनुमति देता है।

किसी विशिष्ट कार्य को करने के लिए उपयोगकर्ता-परिभाषित फ़ंक्शन समूह कोड और कोड के समूह को एक नाम (पहचानकर्ता) दिया जाता है।

जब फ़ंक्शन को प्रोग्राम के किसी भी भाग से आमंत्रित किया जाता है, तो यह सभी फ़ंक्शन के शरीर में परिभाषित कोड निष्पादित करता है।

C ++ फ़ंक्शन घोषणा

फ़ंक्शन घोषित करने का सिंटैक्स है:

 returnType functionName (parameter1, parameter2,… ) ( // function body )

यहाँ एक फ़ंक्शन घोषणा का एक उदाहरण है।

 // function declaration void greet() ( cout << "Hello World"; )

यहाँ,

  • फ़ंक्शन का नाम है greet()
  • फ़ंक्शन का वापसी प्रकार है void
  • खाली कोष्ठक का मतलब है कि इसका कोई पैरामीटर नहीं है
  • फंक्शन बॉडी के अंदर लिखा होता है ()

नोट: हम इस ट्यूटोरियल के बारे में returnTypeऔर parametersबाद में जानेंगे ।

एक समारोह बुला रहा है

उपरोक्त कार्यक्रम में, हमने एक फ़ंक्शन का नाम घोषित किया है greet()greet()फ़ंक्शन का उपयोग करने के लिए , हमें इसे कॉल करने की आवश्यकता है।

यहां बताया गया है कि हम उपरोक्त greet()फ़ंक्शन को कैसे कॉल कर सकते हैं ।

 int main() ( // calling a function greet(); )
C ++ में फंक्शन कैसे काम करता है

उदाहरण 1: एक पाठ प्रदर्शित करें

 #include using namespace std; // declaring a function void greet() ( cout << "Hello there!"; ) int main() ( // calling the function greet(); return 0; )

आउटपुट

 नमस्ते!

फ़ंक्शन पैरामीटर

जैसा कि ऊपर उल्लेख किया गया है, एक फ़ंक्शन को मापदंडों (तर्कों) के साथ घोषित किया जा सकता है। एक पैरामीटर एक मान है जिसे फ़ंक्शन घोषित करते समय पारित किया जाता है।

उदाहरण के लिए, हम नीचे दिए गए फ़ंक्शन पर विचार करते हैं:

 void printNum(int num) ( cout << num; )

यहां, intचर संख्या फ़ंक्शन पैरामीटर है।

हम फ़ंक्शन कॉल करते समय फ़ंक्शन पैरामीटर के लिए एक मान पास करते हैं।

 int main() ( int n = 7; // calling the function // n is passed to the function as argument printNum(n); return 0; )

उदाहरण 2: पैरामीटर के साथ फ़ंक्शन

 // program to print a text #include using namespace std; // display a number void displayNum(int n1, float n2) ( cout << "The int number is " << n1; cout << "The double number is " << n2; ) int main() ( int num1 = 5; double num2 = 5.5; // calling the function displayNum(num1, num2); return 0; )

आउटपुट

 इंट संख्या 5 है डबल संख्या 5.5 है

उपरोक्त कार्यक्रम में, हमने एक फ़ंक्शन का उपयोग किया है जिसमें एक intपैरामीटर और एक doubleपैरामीटर है।

हम फिर num1 और num2 को तर्क के रूप में पास करते हैं। इन मानों को क्रमशः फंक्शन पैरामीटर n1 और n2 द्वारा संग्रहीत किया जाता है।

मापदंडों के साथ C ++ फ़ंक्शन

नोट: फ़ंक्शन को कॉल करते समय पारित किए गए तर्कों का प्रकार फ़ंक्शन घोषणा में परिभाषित संबंधित मापदंडों के साथ मेल खाना चाहिए।

वापसी विवरण

उपरोक्त कार्यक्रमों में, हमने फ़ंक्शन घोषणा में शून्य का उपयोग किया है। उदाहरण के लिए,

 void displayNumber() ( // code )

इसका मतलब यह है कि फ़ंक्शन कोई मान नहीं लौटा रहा है।

किसी फ़ंक्शन से मान वापस करना भी संभव है। इसके लिए, हमें returnTypeफ़ंक्शन घोषणा के दौरान फ़ंक्शन को निर्दिष्ट करना होगा ।

फिर, returnकिसी फ़ंक्शन से मान वापस करने के लिए स्टेटमेंट का उपयोग किया जा सकता है।

उदाहरण के लिए,

 int add (int a, int b) ( return (a + b); )

यहां, हमारे पास intइसके बजाय डेटा प्रकार है void। इसका मतलब है कि फ़ंक्शन एक intमान लौटाता है ।

return (a + b);फ़ंक्शन मान के रूप में कोड दो मापदंडों का योग देता है।

returnवक्तव्य को दर्शाता है कि समारोह समाप्त हो गया है। returnफ़ंक्शन के अंदर किसी भी कोड को निष्पादित नहीं किया जाता है।

उदाहरण 3: दो नंबर जोड़ें

 // program to add two numbers using a function #include using namespace std; // declaring a function int add(int a, int b) ( return (a + b); ) int main() ( int sum; // calling the function and storing // the returned value in sum sum = add(100, 78); cout << "100 + 78 = " << sum << endl; return 0; )

आउटपुट

 100 + 78 = 178

उपरोक्त कार्यक्रम में, add()फ़ंक्शन का उपयोग दो संख्याओं के योग को खोजने के लिए किया जाता है।

हम दो पारित intशाब्दिक 100और 78फ़ंक्शन को कॉल करते हैं।

हम चर राशि में फ़ंक्शन के दिए गए मान को संग्रहीत करते हैं, और फिर हम इसे प्रिंट करते हैं।

रिटर्न स्टेटमेंट के साथ C ++ फंक्शन का कार्य करना

ध्यान दें कि योग एक intप्रकार का चर है । इसका कारण यह है की वापसी मान है add()की है intप्रकार।

समारोह प्रोटोटाइप

In C++, the code of function declaration should be before the function call. However, if we want to define a function after the function call, we need to use the function prototype. For example,

 // function prototype void add(int, int); int main() ( // calling the function before declaration. add(5, 3); return 0; ) // function definition void add(int a, int b) ( cout << (a + b); )

In the above code, the function prototype is:

 void add(int, int);

This provides the compiler with information about the function name and its parameters. That's why we can use the code to call a function before the function has been defined.

The syntax of a function prototype is:

 returnType functionName(dataType1, dataType2,… );

Example 4: C++ Function Prototype

 // using function definition after main() function // function prototype is declared before main() #include using namespace std; // function prototype int add(int, int); int main() ( int sum; // calling the function and storing // the returned value in sum sum = add(100, 78); cout << "100 + 78 = " << sum << endl; return 0; ) // function definition int add(int a, int b) ( return (a + b); )

Output

 100 + 78 = 178

The above program is nearly identical to Example 3. The only difference is that here, the function is defined after the function call.

That's why we have used a function prototype in this example.

Benefits of Using User-Defined Functions

  • Functions make the code reusable. We can declare them once and use them multiple times.
  • Functions make the program easier as each small task is divided into a function.
  • Functions increase readability.

C++ Library Functions

Library functions are the built-in functions in C++ programming.

Programmers can use library functions by invoking the functions directly; they don't need to write the functions themselves.

Some common library functions in C++ are sqrt(), abs(), isdigit(), etc.

In order to use library functions, we usually need to include the header file in which these library functions are defined.

For instance, in order to use mathematical functions such as sqrt() and abs(), we need to include the header file cmath.

Example 5: C++ Program to Find the Square Root of a Number

 #include #include using namespace std; int main() ( double number, squareRoot; number = 25.0; // sqrt() is a library function to calculate the square root squareRoot = sqrt(number); cout << "Square root of " << number << " = " << squareRoot; return 0; )

Output

 25 = 5 का वर्गमूल

इस कार्यक्रम में, sqrt()संख्या के वर्गमूल की गणना करने के लिए लाइब्रेरी फ़ंक्शन का उपयोग किया जाता है।

फ़ंक्शन की घोषणा हेडर फ़ाइल sqrt()में परिभाषित की गई है cmath। इसलिए हमें फ़ंक्शन #include का उपयोग करने के लिए कोड का उपयोग करने की आवश्यकता sqrt()है।

अधिक जानने के लिए, C ++ Standard Library फ़ंक्शन पर जाएं।

दिलचस्प लेख...