User-Defined Functions

Writing your own reusable blocks of code in C++.

User-Defined Functions

User-Defined Function एक ऐसा function है जिसे programmer अपनी आवश्यकता के अनुसार स्वयं define करता है। इसका उपयोग किसी specific task (विशिष्ट कार्य) को अलग-अलग स्थानों पर बार-बार करने के लिए किया जाता है। इससे program को सरल, व्यवस्थित और reusable बनाया जा सकता है।

याद रखें: User-Defined Function वह function है जिसे programmer स्वयं बनाता और define करता है ताकि किसी विशेष कार्य को आसानी से perform किया जा सके।

What is a User-Defined Function?

C++ में functions का उपयोग program को छोटे-छोटे logical parts में divide करने के लिए किया जाता है। C++ में कुछ functions पहले से उपलब्ध होते हैं, जैसे sqrt(), जबकि programmer अपनी आवश्यकता के अनुसार अपने functions भी बना सकता है। ऐसे functions को User-Defined Functions कहा जाता है।

उदाहरण के लिए, यदि किसी program में किसी number का square कई बार निकालना है, तो square निकालने के लिए एक अलग function बनाया जा सकता है।

#include <iostream>
using namespace std;

int square(int n)
{
    return n * n;
}

int main()
{
    cout << square(5);

    return 0;
}
Output:
25

Need for User-Defined Functions

बड़े programs में यदि पूरा code एक ही main() function में लिखा जाए, तो program को समझना और maintain करना कठिन हो सकता है। User-defined functions program को छोटे और meaningful parts में divide करने में सहायता करते हैं।

इनका उपयोग निम्नलिखित कारणों से किया जाता है:

  • Program को छोटे modules में divide करने के लिए
  • Code को बार-बार reuse करने के लिए
  • Program को सरल और readable बनाने के लिए
  • Code duplication को कम करने के लिए
  • Program को debug और maintain करने में आसानी के लिए
  • किसी specific task को अलग-अलग स्थानों से call करने के लिए

Parts of a User-Defined Function

एक सामान्य user-defined function में मुख्य रूप से निम्न parts होते हैं:

  1. Function Declaration / Prototype
  2. Function Definition
  3. Function Call
Basic Structure:
Function Declaration → Function Definition → Function Call

Function Declaration

Function Declaration को function prototype भी कहा जाता है। इसमें compiler को function के name, return type और parameters के बारे में जानकारी दी जाती है।

सामान्य syntax:

return_type function_name(parameter_list);

उदाहरण:

int add(int, int);

यह declaration बताता है कि add() नाम का function integer value return करेगा और इसमें दो integer parameters होंगे।

Function Definition

Function Definition में function का actual code लिखा जाता है। इसमें यह निर्धारित किया जाता है कि function को call करने पर कौन-सा कार्य किया जाएगा।

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

ऊपर दिए गए function में a और b parameters हैं और function दोनों का sum return करता है।

Function Call

किसी function को execute करने के लिए उसे call करना पड़ता है। Function call में function का नाम और आवश्यक arguments दिए जाते हैं।

int result = add(10, 20);

यहाँ add() function को 10 और 20 arguments के साथ call किया गया है।

Complete Example of User-Defined Function

#include <iostream>
using namespace std;

int add(int a, int b);

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

int main()
{
    int result = add(10, 20);

    cout << "Sum = " << result;

    return 0;
}
Output:
Sum = 30

Function with No Return Value

यदि कोई function कोई value return नहीं करता है, तो उसके return type के रूप में void का उपयोग किया जाता है।

#include <iostream>
using namespace std;

void display()
{
    cout << "Welcome to C++";
}

int main()
{
    display();

    return 0;
}
Output:
Welcome to C++

यहाँ display() function केवल message display करता है और कोई value return नहीं करता है। इसलिए इसका return type void है।

Function with Parameters

Function को input देने के लिए parameters का उपयोग किया जाता है। Parameters function definition में लिखे जाते हैं।

#include <iostream>
using namespace std;

void greet(string name)
{
    cout << "Hello " << name;
}

int main()
{
    greet("Amit");

    return 0;
}
Output:
Hello Amit

Function with Return Value

ऐसा function जो processing के बाद कोई value वापस करता है, उसे return value वाला function कहा जा सकता है। Return value देने के लिए return statement का उपयोग किया जाता है।

#include <iostream>
using namespace std;

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

int main()
{
    int result = multiply(6, 5);

    cout << "Product = " << result;

    return 0;
}
Output:
Product = 30

Function with Parameters and Return Value

एक function parameters भी accept कर सकता है और processing के बाद value भी return कर सकता है। यह functions का एक बहुत common form है।

int square(int n)
{
    return n * n;
}

यहाँ:

  • int → Return type
  • square → Function name
  • int n → Parameter
  • return n * n; → Return statement

Function with No Parameters

किसी function में कोई parameter न भी हो सकता है। ऐसे function में parameter list खाली रहती है।

#include <iostream>
using namespace std;

void message()
{
    cout << "C++ Programming";
}

int main()
{
    message();

    return 0;
}
Output:
C++ Programming

Function with Multiple Parameters

एक function में एक से अधिक parameters भी हो सकते हैं। Parameters को comma से separate किया जाता है।

#include <iostream>
using namespace std;

int calculate(int a, int b, int c)
{
    return a + b + c;
}

int main()
{
    cout << calculate(10, 20, 30);

    return 0;
}
Output:
60

Arguments and Parameters

Parameters वे variables होते हैं जो function definition में लिखे जाते हैं। जबकि Arguments वे actual values होती हैं जो function को call करते समय pass की जाती हैं।

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

int result = add(10, 20);

इस उदाहरण में:

  • a और b → Parameters
  • 10 और 20 → Arguments
Term Meaning Example
Parameter Function definition में दिए गए variables int a, int b
Argument Function call के समय दी गई actual values 10, 20

Return Statement

return statement का उपयोग function से value वापस भेजने के लिए किया जाता है। Function का return type और returned value का type compatible होना चाहिए।

int maximum(int a, int b)
{
    if (a > b)
        return a;
    else
        return b;
}

यदि maximum(15, 25) call किया जाए, तो function 25 return करेगा।

Calling a Function Multiple Times

User-defined function का एक महत्वपूर्ण लाभ यह है कि इसे program में एक से अधिक बार call किया जा सकता है। इससे same code को बार-बार लिखने की आवश्यकता नहीं होती।

#include <iostream>
using namespace std;

int square(int n)
{
    return n * n;
}

int main()
{
    cout << square(2) << endl;
    cout << square(4) << endl;
    cout << square(6);

    return 0;
}
Output:
4
16
36

Function Prototype

यदि function को main() से पहले define नहीं किया गया है, तो compiler को function की जानकारी देने के लिए function prototype का उपयोग किया जा सकता है।

#include <iostream>
using namespace std;

int add(int, int);

int main()
{
    cout << add(5, 7);

    return 0;
}

int add(int a, int b)
{
    return a + b;
}
Output:
12
याद रखें: Function prototype compiler को function के return type, name और parameters की जानकारी देता है।

Advantages of User-Defined Functions

  • Reusability: एक function को कई बार call किया जा सकता है।
  • Modularity: बड़े program को छोटे modules में divide किया जा सकता है।
  • Readability: Program को पढ़ना और समझना आसान होता है।
  • Debugging: Errors को किसी specific function में आसानी से locate किया जा सकता है।
  • Maintenance: Program में changes करना आसान होता है।
  • Code Reduction: Repeated code को function में रखकर program को छोटा किया जा सकता है।

User-Defined Function and Built-in Function

User-Defined Function Built-in / Library Function
Programmer द्वारा बनाया जाता है। Library में पहले से उपलब्ध होता है।
Program की आवश्यकता के अनुसार define किया जाता है। Predefined functionality प्रदान करता है।
Example: add(), square() Example: sqrt(), strlen()

Example: Function to Find Even or Odd

एक user-defined function की सहायता से किसी number के even या odd होने की जाँच की जा सकती है।

#include <iostream>
using namespace std;

void checkEvenOdd(int n)
{
    if (n % 2 == 0)
        cout << "Even";
    else
        cout << "Odd";
}

int main()
{
    checkEvenOdd(25);

    return 0;
}
Output:
Odd

Example: Function to Find Factorial

User-defined function का उपयोग factorial निकालने के लिए भी किया जा सकता है।

#include <iostream>
using namespace std;

int factorial(int n)
{
    int fact = 1;

    for (int i = 1; i <= n; i++)
    {
        fact = fact * i;
    }

    return fact;
}

int main()
{
    cout << "Factorial = " << factorial(5);

    return 0;
}
Output:
Factorial = 120

Important Points

  • User-defined function programmer द्वारा स्वयं बनाया जाता है।
  • Function का उपयोग किसी specific task को perform करने के लिए किया जाता है।
  • Function declaration को function prototype भी कहा जाता है।
  • Function definition में actual statements लिखे जाते हैं।
  • Function को execute करने के लिए उसे call करना आवश्यक है।
  • Function parameters accept कर सकता है।
  • Function value return कर सकता है या void होने पर कोई value return नहीं कर सकता।
  • return statement function से value वापस भेजने के लिए उपयोग होता है।
  • एक function को program में कई बार call किया जा सकता है।
  • User-defined functions program की modularity और reusability बढ़ाते हैं।

Board Focus

Exam के लिए याद रखें:
User-Defined Function → Programmer द्वारा बनाया गया function
Function Prototype → Function की declaration
Function Definition → Function का actual code
Function Call → Function को execute करने के लिए call करना
Parameter → Function definition में variable
Argument → Function call के समय actual value
return → Function से value वापस करना
void → कोई value return नहीं करना

Board Important Questions

Very Short Answer Questions

Q1. User-Defined Function क्या है?

Answer: Programmer द्वारा अपनी आवश्यकता के अनुसार बनाया गया function User-Defined Function कहलाता है।

Q2. Function Prototype क्या है?

Answer: Function की declaration को Function Prototype कहा जाता है।

Q3. Function Call क्या है?

Answer: किसी function को execute करने के लिए उसे call करना Function Call कहलाता है।

Q4. Function में void का क्या अर्थ है?

Answer: void बताता है कि function कोई value return नहीं करता है।

Q5. Parameter और Argument में क्या अंतर है?

Answer: Function definition में दिए गए variables parameters होते हैं, जबकि function call के समय दी गई actual values arguments होती हैं।

Q6. Function से value वापस करने के लिए किस statement का उपयोग किया जाता है?

Answer: return statement का उपयोग किया जाता है।

Short Answer Questions

Q7. User-Defined Functions के कोई चार लाभ लिखिए।

Answer: User-defined functions के प्रमुख लाभ हैं—code reusability, modularity, readability और easy debugging। इनके कारण program को maintain करना भी आसान होता है।

Q8. Function Declaration, Definition और Call को समझाइए।

Answer: Function Declaration में compiler को function की जानकारी दी जाती है। Function Definition में function का actual code लिखा जाता है। Function Call के माध्यम से function को execute किया जाता है।

Q9. Parameter और Argument को उदाहरण सहित समझाइए।

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

add(10, 20);

यहाँ a और b parameters हैं, जबकि 10 और 20 arguments हैं।

Q10. User-Defined Function का उपयोग क्यों किया जाता है?

Answer: User-defined functions का उपयोग program को modular, readable और reusable बनाने के लिए किया जाता है। इससे repeated code को एक function में रखा जा सकता है और उसे आवश्यकता के अनुसार कई बार call किया जा सकता है।

Long Answer Questions

Q11. User-Defined Function को उसके विभिन्न parts के साथ समझाइए।

Answer: User-defined function programmer द्वारा बनाया गया function होता है। इसके मुख्य parts Function Declaration, Function Definition और Function Call हैं। Declaration में function की जानकारी दी जाती है, definition में actual statements लिखे जाते हैं और call के द्वारा function को execute किया जाता है।

Q12. दो numbers का sum निकालने के लिए user-defined function का C++ program लिखिए।

#include <iostream>
using namespace std;

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

int main()
{
    int result = add(15, 25);

    cout << "Sum = " << result;

    return 0;
}
Output:
Sum = 40

Q13. User-defined function की सहायता से किसी number का factorial निकालने का program लिखिए।

#include <iostream>
using namespace std;

int factorial(int n)
{
    int fact = 1;

    for (int i = 1; i <= n; i++)
    {
        fact *= i;
    }

    return fact;
}

int main()
{
    cout << factorial(5);

    return 0;
}
Output:
120

Q14. निम्न program का output बताइए:

#include <iostream>
using namespace std;

int square(int n)
{
    return n * n;
}

int main()
{
    cout << square(4);

    return 0;
}
Output:
16

Q15. निम्न program का output बताइए:

#include <iostream>
using namespace std;

void display()
{
    cout << "Hello C++";
}

int main()
{
    display();

    return 0;
}
Output:
Hello C++

Quick Revision

  • User-Defined Function: Programmer द्वारा बनाया गया function
  • Declaration: Function की prototype/declaration
  • Definition: Function का actual code
  • Call: Function को execute करना
  • Parameter: Function definition में variable
  • Argument: Function call में actual value
  • return: Function से value वापस करना
  • void: कोई value return न करने वाला function
One-Line Revision: User-Defined Function programmer द्वारा बनाया गया reusable code block है, जिसका उपयोग program को modular, readable और efficient बनाने के लिए किया जाता है।

Practice Questions

  1. User-Defined Function की परिभाषा लिखिए।
  2. Function Declaration और Function Definition में अंतर बताइए।
  3. Function Call क्या है?
  4. Parameter और Argument में अंतर बताइए।
  5. void function क्या होता है?
  6. return statement का क्या उपयोग है?
  7. User-defined functions के चार लाभ लिखिए।
  8. दो numbers का sum निकालने के लिए user-defined function का C++ program लिखिए।
  9. किसी number का square निकालने के लिए user-defined function बनाइए।
  10. किसी number के even या odd होने की जाँच करने के लिए user-defined function लिखिए।
  11. Factorial निकालने के लिए user-defined function का program लिखिए।
  12. Function prototype क्या है? उदाहरण सहित समझाइए।
  13. एक ऐसे function का उदाहरण दीजिए जिसमें parameters हों लेकिन return value न हो।
  14. एक ऐसे function का उदाहरण दीजिए जिसमें parameters और return value दोनों हों।
  15. User-defined functions program की modularity और reusability को कैसे बढ़ाते हैं?
Lesson 30 of 39
On This Page