हिंदी माध्यम नोट्स
C++ : User define function ( Part -1 ) in hindi , what is User define function in c++ language with example program
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 करेगे |
Recent Posts
सती रासो किसकी रचना है , sati raso ke rachnakar kaun hai in hindi , सती रासो के लेखक कौन है
सती रासो के लेखक कौन है सती रासो किसकी रचना है , sati raso ke…
मारवाड़ रा परगना री विगत किसकी रचना है , marwar ra pargana ri vigat ke lekhak kaun the
marwar ra pargana ri vigat ke lekhak kaun the मारवाड़ रा परगना री विगत किसकी…
राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए sources of rajasthan history in hindi
sources of rajasthan history in hindi राजस्थान के इतिहास के पुरातात्विक स्रोतों की विवेचना कीजिए…
गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है ? gurjaratra pradesh in rajasthan in hindi
gurjaratra pradesh in rajasthan in hindi गुर्जरात्रा प्रदेश राजस्थान कौनसा है , किसे कहते है…
Weston Standard Cell in hindi वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन
वेस्टन मानक सेल क्या है इससे सेल विभव (वि.वा.बल) का मापन Weston Standard Cell in…
polity notes pdf in hindi for upsc prelims and mains exam , SSC , RAS political science hindi medium handwritten
get all types and chapters polity notes pdf in hindi for upsc , SSC ,…