C++ : User define function ( Part -1 ) in hindi , what is User define function in c++ language with example program

what is User define function in c++ language with example program , C++ : User define function ( Part -1 ) in hindi :-
इससे पहले के article मे data type (array , integer , character , string , enum आदि ) , control और branching statement (if statement , switch statement आदि ) और looping statement ( for loop , while loop और do-while loop ) को discuss किया गया था | अब आगे के article मे function , function के type , return type आदि को discuss करेगे |

function
जब किसी प्रोग्राम के length बहुत बड़ी होती है तब in लम्बे प्रोग्राम को छोटे छोटे भागो मे divind कर देते है |in छोटे भाग को function कहते है | in function को प्रोग्राम मे कभी कभी use किया जा सकता है | अतः function प्रोग्राम का छोटा सा भाग होता है जिसे कभी use किया जा सकता है | function को दो भागो मे divind किया गया है :-
library function
library function , एसे function होता है जो पहले ही define किया जाता है | library function कंप्यूटर memory मे जहा पर store होता है उस जगह header file कहते है | library function को प्रोग्राम को use करने से पहले header file को main function से बहार include कर दिया जाता है | उदाहरण के लिए pow() एक library function है जिसे math.h header file मे  define है इसका उदाहरन निन्म है :
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
using namespace std;
int base ,power   ;
cout<<“Enter Base : “;
cin>>base;
cout<<“Enter Power : “;
cin>>power;
int v1 = power (base,power);
base=11;
power = 2;
int v1 = power (base,power);
cout<<“first power Expression output :”<<v1<<endl
cout<<“Second power Expression output :”<<v2 <<endl;
}
getch();
}
इस उदाहर मे देख सकते है |
pow() function को use करने के लिए , हेडर field मे math.h header file को include किया गया है | उसके बाद main() function मे दो variable को declare किया गया है  | इसमें से एक power और base की value को hold करता है |  और दो statement मे pow() को use करने power expression के value को calculate किया जाता है |
इसका आउटपुट होता है :
Enter Base : 12
Enter Power : 3
first power Expression output :
Second power Expression output :

User Define Function
जब किसी function को यूजर द्वारा define किया जाता है उसे user define function कहते है | किसी sepecfic task को perform करने के लिए यूजर द्वारा function को declare किया जा सकता है |  in function को use करने के लिए किसी भी हेडर file को include नहीं करना पड़ता है |  User Define Function को प्रोग्राम मे कही पर भी use किया जा सकता है |

User Define Function कैसे कार्य करता है ?
जब किसी प्रोग्राम को execute किया जाता है  तब complier main() function को call किया जाता है | और main() function का execution start हो जाता है | जब complier control , User Define Function तक पहुचता है तब complier control , User Define Function के डेफिनिशन पर चला जाता है | और User Define Function execute होता है | उसके बाद  complier control main() function पर आ जाता है | और बाकि का main() execute होता है |

user-defined function के लिए निन्म उदाहरन है :-

#include <iostream>
#include<conio.h>
using namespace std;]
// Function prototype (declaration)
int add(int, int);
int main()
{
int num1, num2, sum;
cout<<“Enters two numbers : “;
cin >> num1 >> num2;
// Function call
sum = add(num1, num2);
cout << “Output : ” << sum;
getch();
}
// Function definition
int add(int a, int b)
{
int output;
output = a + b;
// Return statement
return output;
}
इस उदाहरन मे , यूजर define function add() को use किया गया है इसके लिए function के तीन भागो  function prototype , function defintion और function call को declare किया गया है | इसका Output होगा :-

Enters two numbers : 8 -4
Output : 4

Function prototype (declaration)
अगर user define function को main() function के बाद define किया जाता है तब complier error message आ जाता है | क्योकि complier ,user define function और इस function मे pass होने वाले argument को भी  नहीं जनता है |
c++ language मे , function prototype , function को declare करता है : इस उदाहरन मे निन्म statement function prototype होता है :-
int add(int, int);
Function prototype मे function की definition include नहीं होता है | इस function मे return type होता है | किसी function prototype को निन्म तरह से declare कर सकते है | function prototype मे  argument मे pass value की type को define किया जाता है |
int add(int a, int b);

Function Call
जब किसी  user define function  को main() मे use किया जाता है तब user define function  को call किया जाता है | उपर की उदाहरन मे निन्म call statement होता है :-
add(num1,num2);
इस function से जो भी value return होगी वो variable sum मे assign होती है |

Function Definition
Function Definition मे , function के द्वारा किये गये task के लॉजिक को define किया जाता है | इस उदाहरन मे निन्म Function Definition है :-
// Function definition
int add(int a, int b)
{
int output;
output = a + b;
// Return statement
return output;
}

इस article मे , function के basic struture को discuss किया है | अब आगे के article मे function के type को discuss करेगे |