what is c++ function in hindi-c++ फंक्शन क्या है?

हेल्लो दोस्तों आज के इस पोस्ट में आपको c++ function in hindi के बारे में बताया गया है की क्या होता है कैसे काम करता है तो चलिए शुरू करते है

c++ function का परिचय

एक function कुछ programming statement का group होता है जो ये statement एक particular task को perform करते है जैसे की addition या फिर area calculate करना आदि हर function का unique नाम होता है इस नाम के द्वारा आप function को program में कही भी प्रयोग कर सकते है इसे function को call करना करते है

function को प्रयोग करने से आपको program में एक ही code को बार बार लिखने की आवश्यकता नहीं होती है आप एक function को create कर सकते है और उसे program में कही भी प्रयोग कर सकते है function को प्रयोग  करने से आपने program ज्यादा readable बन जाता अहै जिसे आसानी से debug किया जा सकता है

c++ में 2 प्रकार के function पाए जाते है

  • built in function –ये वे function होते है जो की c++ आपको पहले से libraries के द्वारा provide करती है
  • user defined functions –ये वे functions होते है जो की programmers खुद create करते है सबसे common user defined function main() होता है इसके बारे में आपको निचे दिया जा रहा है

main() function

main() function आपके program का starting point होता है जो की c language में main() function का return type नहीं specify किया जाता है लेकिन c++ में main() function integer को return करती है इसलिए c++ में आप main() function का return type देते है इसका example आपको निचे दिया जा रहा है

int main()

जब भी किसी function का return type define किया जाता है तो function एक value को return करती है इसलिए आप main() function के अंत में return zero statement define करते है जिसका example आपको निचे दिया जा रहा है

return 0;

creating function in c++

c++ में आप functions 2 steps में create करते है

  1. function declaration/prototyping
  2. function definition

इन steps के बारे में आपको निचे दिया जा रहा है

function declaration /prototyping

जब भी आप c++ में कोई function create करते है तो सबसे पहले आप function prototype define करते है function prototype compiler को function के बारे में basic जानकारी provide करती है जैस की

  1. return type-function किस तरह की value return करेगा
  2. name-function को किस नाम से call किया जायेगा
  3. parameters-function में किस type के और कितने argument को pass किये जा सकता है

इस जानकारी को compiler function को call और execute करने के लिए प्रयोग करता है तो आएये अब देखते है की आप किस तरह एक function prototype define कर सकते है जिसका आपको general syntax निचे दिया जा रहा है

return_type function-name(parameter1,parameter2,.....parameter n);

जब भी आप function का prototype define करते है तो सबसे पहले function return type define करते है ये कोई भी data type हो सकता है जैस की int ,float आदि इसके बाद आप function का एक unique नाम देते है इसके बाद आप parameter को define करते है ये वो value होती है तो की आप function को call करते समय pass करेगे parameter के साथ आप उनका type भी define करते है तो आएये इसे एक example के समझने का प्रयास करते है

मान लीजिये की आप एक function को create कर रहे है तो जो 2 integer number को argument के रूप में लेता है तो और उनको multiply करके result return करती है तो इस function के लिए prototype आप इस प्रकार define कर सकते है

int mul(int a,int b);

function definition

function definition एक block होता है जो की जिसमे आप उन statement को लिखते है जिन्हें आप function call होने पर execute करना चाहते है तो function definition को function declaration /prototype की तरह ही define किया जाता है लेकिन इसे semicolon से end नहीं किया जाता है और curly braces के अन्दर आप वो statement को लिखते है जिन्हें आप execute करना चाहते है function definition का structure आपको निचे दिया जा रहा है

return-type function-name(parameter1,parameter2,....parameter n)
{
      //statement
}

तो आएइए अब इसको example से द्वारा समझते है निचे उसी function की definition को create की गयी है जिसका prototype उपर create किया गया है

int mul(int a ,int b)
{
     return a*b;
}

using function

एक बार function को create करने के बाद आप उसे प्रयोग करते है program में जहा भी आप function को प्रयोग करना चाहते है तो वहा आप उसे call करते है तो function को call करने समय आप उसमे required arguments को pass करते है जो की इन argument का type वाही होना चाहिए जो की आपने parameter को define करते समय दिया था नहीं तो compiler को error generate करता है

यदि किसी function में argument नहीं pass करने हो तो आप simply brackets लगा कर semicolon लगा देते है function call करने का basic syntax निचे दिया जा रहा है

function-name(argument1,argument2,....argument n);

example के लिए यदि उपर create किये गए function को call करना चाहते है तो इस इस प्रकार करेंगे

mul(3,8);

उपर दिए गए example में mul() function को call किया गया है function को declare करते समय parameter का type int दिया गया था इसलिए यहाँ पर मैंने दो integer value pass की है तो ये function इन दोनों value को multiply करके result return करेगा

आप चाहे तो directly value न pass करते हुए 2 integer variable को भी pass कर सकते है जैसे की निचे दिया गए example को देखिये

int a=3;
int b=8;
mul(a,b);

यदि आप उपर दिए गए example की तरह भी function को call करेंगे तो result same ही होगा निचे function के प्रयोग का एक complete example दिया जा रहा है

#include<iostream>
Int mul(int a,int b);
Int main()
{
      Int a=2;
      Int b=3;
       Cout<<”multiplication of 2 & 3 is :”<<mul(a,b);
}
Int mul(int a,int b)
{
    Return a*b;
}

उपर दिया गया program निचे दिया गया output को generate करता है

multiplication of 2 & 3 is :6

call by reference

जब भी आप किसी function को call करते समय जो value pass करते है तो वो parameter variable की assign हो जाती है जितने भी change होते है वो इन parameter variable पर ही होते है और जो actual variable होते है उनकी value में कोई change नहीं होता है

example के लिए मान लीजिये की आपने के  increment() function को create किया है जो variable की value को 1 number से increase करता है

void increment(int a)
{
    a=a+1;
}

उपर create किये गए function को call करते समय इसमे आप इस प्रकार एक variable को pass करेंगे

int a=10;
increment(a);

function की definition के  हिसाब से जब function call हो तो a variable की value 10 से 11 हो जानी चाहिए लेकिन ऐसा नहीं होगा function के अन्दर variable में जो change किया गया है वह parameter variable को effect करेगा यानी की इस a की value को सिर्फ function में ही 11 होगी function के बाहर original variable वैसे का वैसा रहेगा यदि आप function से ही a की value print करवाते है तो value 11 ही print होगी

कई बार programming करते समय ऐसा situation भी आती है की आपको original variable की value change करनी होती है example के लिए यदि कोई वेबसाइट अपने visitors की सख्या cout करने के लिए function को create करती है तो जरुरी है की change original variable पर हो

ऐसी situation में आप c++ का call by reference feature का प्रयोग कर सकते है c++ आपको function में actual variable का reference pass करने की capabilities को provide करती है ऐसा आप address-of(&) operator के द्वारा करते है

जब आप reference के द्वारा function में parameter variable को declare करते है तो ये variable भी उसी मेमोरी location को point करते है जिसे original variable point करते है ऐसे में जब आप इन variable पर कोई change perform करते है तो original variable भी change हो जाती है

function को reference के द्वारा call करने के लिए आप parameter variable के पहले address of operator लगते है जिसका आपको example निचे दिया जा रहा है

void increment(int &a)
{
     a=a+1;
}

इस function को आप इस प्रकार call करेगे

int a=10;
increment(a);

जब आप increment function को call करते है तो a variable की value की यहाँ पर original variable है change होकर 11 हो जाएगी इसे निचे एक complete example के द्वारा समझाया जा रहा है

#include<iostream>
Using namespace std;
Void increment(int &a);
Int main()
{
    Int a=10;
     Increment(a);
    Cout<<”value of after function call is <<a;
}
Void increment(int &a)
{
      a=a+1;
}

उपर दिए गए program में निचे दिया गया output को generate करता है

value of a after function call is : 11

reference-https://www.tutorialspoint.com/cplusplus/cpp_functions.htm

निवेदन:-आप सभी छात्र –छात्रों से निवेदन है की अगर आपको ये Topic(c++ function in hindi) या post अच्छा लगा हो तो कृपया आप इस वेबसाइट के बारे में अपने दोस्तों को जरुर बताये और -अगर कोई topic(c++ function in hindi) से संबधित प्रश्न हो तो कमेंट्स( c++ function in hindi) आपके लिए ही बना है और किसी Subject(c++ function in hindi) के लेकर भी कोई प्रश्न हो तो कमेंट करे

2 thoughts on “what is c++ function in hindi-c++ फंक्शन क्या है?”

Leave a Comment