what is Inline Function in c++ language example program , C++ : Inline Function in hindi :-
इससे पहले के article मे function को discuss किया जाता है | अब इस article मे c++ की एक और property को discuss करेगे | जिसे inline function कहते है | inline function के कार्य function के सामान होता है |
Inline Function
c++ मे inline function का use , function की speed को बढ़ाने के लिए किया जाता है | inline function और नार्मल function मे कोई भी difference नहीं होता है | लेकिन इन function को call करने के लिए नए method को use किया जाता है | inline function और normal function मे difference को समजने के लिए निन्म पॉइंट्स को consider किया जाता है |
किसी प्रोग्राम को execute करने के लिए बहुत सारे machine codes को use किया जाता है | जब किसी प्रोग्राम को start किया जाता है तब operating सिस्टम , कंप्यूटर memory मे instructions को लोड करता है | प्रत्येक instructions का कंप्यूटर memory मे space को allocate किया जाता है | कंप्यूटर control, इन instructions one by one execute करता है | लेकिन अगर कोई braching statement आ जाता है तब कुछ instructions को स्किप भी किया जा सकता है | और कोई forward और backward instructions मे jump भी किया जा सकता है |
जब Normal function को call किया जाता है तब प्रोग्राम control , main() के instructions को function के instructions मे jump होता है और instructions के execute होने के बाद वापिस main()मे return हो जाता है | इस प्रोसेस को और अच्छी तरह से discuss करते है | जब प्रोग्राम control function call के instructions तक पहुचता है तब instructions के memory address को function call के बाद load हो जाता है | अतः function का argument को stack मे assign किया जाता है | और प्रोग्राम control को function के instructions मे jump हो जाता है |और प्रोग्राम control function के instructions के बाद stack मे store instructions अपर jump हो जाता है |
c++ inline function का method , नार्मल function से अलग अलग होता है | C++ language मे , inline function मे main() के साथ दूसरा function inline रहता है | अतः inline function को प्रोग्राम मे function call के साथ replace किया जाता है |inline function से , जब inline function को call किया जाता है तब प्रोग्राम control, inline function पर jump नहीं होता है | इसलिए प्रोग्राम की speed बढ़ जाती है | और inline function से memory loss को भी कम किया जाता है |
inline function को use करने के लिए बहुत पॉइंट्स ध्यान मे रखना पड़ता है | inline function को execute करने के लिए लगा time तो बढ़ जाता है |अगर inline function की length बहुत कम होती है तब inline function को use करना बेकार होता है | इसलिए बड़े बड़े function के लिए inline function को use करना चाहये जिससे प्रोग्राम मे memory occupation और time दोनों ही कम हो जाये |
inline function को use करने के लिए निन्म स्य्ताक्स को use किया जाता है :-
inline function_name (argument);
inline function_name (argument)
{
function_body ;
}
इस syntax मे function declartion और function definition मे inline keyword को use किया जाता है |
इस का उदाहरन होता है :
इस उदाहरण मे , inline function से calculator को बनाया गया है |
Expalnation
सबसे पहले यूजर द्वारा choice को input किया जाता है |
Enter ‘+’ for addition
Enter ‘-‘ for subtraction
Enter ‘*’ for multiplication
Enter ‘/’ for division
उसके बाद यूजर से choice को input करा लेते है | और इसके बाद यूजर से values को input करा लेते है |
choice को switch statement मे pass किया जाता है | उस आधार पर function को कैल्ल किया जाता है |
inline add() मे ,
inline add() function मे दो value को addition किया जाता है | और आउटपुट की value को display किया जाता है |
inline sub() मे ,
inline sub () function मे दो value को subtraction किया जाता है | और आउटपुट की value को display किया जाता है |
inline mul() मे ,
inline mul() function मे दो value को multilication किया जाता है | और आउटपुट की value को display किया जाता है |
inline div() मे ,
inline div () function मे दो value को division किया जाता है | और आउटपुट की value को display किया जाता है |
Source Code
#include <iostream>
#include<conio.h>
using namespace std;
inline void add(int, int );
inline void sub(int, int );
inline void mul(int, int );
inline void div(int, int );
void main()
{
int number1 , number2 ;
char choice ;
cout<<“Enter ‘+’ for addition”<<endl<<“Enter ‘-‘ for subtraction”<<endl<<“Enter ‘*’ for multiplication”,,endl<<“Enter ‘/’ for division”<<endl;
cout<<“Enter choice : “;
cin >> choice;
cout<<“Enter number1 : “;
cin >> number1;
cout<<“Enters number2 : “;
cin >> number2;
switch(choice)
{
case ‘+’ :
add(number1,number2);
break;
case ‘-‘ :
sub(number1,number2);
break;
case ‘*’ :
mul(number1,number2);
break;
case ‘/’ :
div(number1,number2);
break;
default :
cout<<“invalid choice “;
break;
}
getch();
}
// Function definition
inline void add(int a, int b)
{
int output;
output = a + b;
cout<<“Output : “<<output;
}
// Function definition
inline void div(int a, int b)
{
int output;
output = a / b;
cout<<“Output : “<<output;
}
// Function definition
inline void sub(int a, int b)
{
int output;
output = a – b;
cout<<“Output : “<<output;
}
// Function definition
inline void mul(int a, int b)
{
int output;
output = a * b;
cout<<“Output : “<<output;
}
इस उदहारण मे चार inline functions को use किया जाता है |
जब add() को call किया जाता है | add() की body main() मे add() call statement पर copy होता है | और execute होता है | इस प्रोसेस मे प्रोग्राम control को shift नहीं किया जाता है |
जब sub() को call किया जाता है | add() की body main() मे sub() call statement पर copy होता है | और execute होता है | इस प्रोसेस मे प्रोग्राम control को shift नहीं किया जाता है |
जब mul() को call किया जाता है | add() की body main() मे mul() call statement पर copy होता है | और execute होता है | इस प्रोसेस मे प्रोग्राम control को shift नहीं किया जाता है |
जब div() को call किया जाता है | add() की body main() मे div() call statement पर copy होता है | और execute होता है | इस प्रोसेस मे प्रोग्राम control को shift नहीं किया जाता है |
इसका आउटपुट होगा :
Enter ‘+’ for addition
Enter ‘-‘ for subtraction
Enter ‘*’ for multiplication
Enter ‘/’ for division
Enter choice : –
Enter number1 : 24
Enter number2 : 12
output : 12
इस article मे inline function को discuss किया गया है अब आगे के article मे class और object को discuss करेगे |