Inline Functions in C++

फंक्शन निर्देशों का समूह होता हैं जिसे इसके नाम के द्वारा अन्‍य फंक्शन में Call किया जाता हैं। जिस फंक्शन में फंक्शन (Function) को Call किया जाता हैं, वह Calling फंक्शन (Function) कहलाता हैं।

जब फंक्शन Call किया जाता हैं तो प्रोग्राम का नियंत्रण Calling फंक्शन (Function) से हटकर Call किये जाने वाले फंक्शन (Function) की Body में चला जाता हैं तथा Call सम्‍पन्‍न होने के बाद Program का नियंत्रण पुन: Calling फंक्शन (Function) में स्‍थानांतरित (आ जाता) हो जाता हैं और Program Flow आगे बढ़ता हैं अत: किसी भी फंक्शन को call करते समय निम्‍न क्रियायें सम्‍पन्‍न होती हैं –

  1. नये Register की स्थिति को एक Stack में Store किया जाता हैं।
  2. Call किये गये फंक्शन (Function) को Argument pass किये जाते हैं।
  3. कार्य सम्‍पन्‍न होने के बाद Register को पुन: स्थिति में वापिस लाया जाता हैं।
  4. Call किये गये फंक्शन (Function) के द्वारा result return किया जाता हैं।

What is Inline Function

फंक्शन (Function) को Call करने की उपरोक्‍त कार्यविधि Program के दृष्टिकोण से कठिन होती हैं क्‍योंकि इसमें Program का Control एक जगह से हटकर दूसरी जगह transfer होता हैं, यदि Program में बहुत अधिक संख्‍या में फंक्शन (Function) प्रयोग किये जाते हैं। तब Program execution बढ़ जाता हैं। फंक्शन (Function) Calling की इस जटिलता को दूर करने के लिये C++ में inline फंक्शन (Function) प्रयोग किये जाते हैं।

Inline फंक्शन (Function) एक Macro की तरह कार्य करता हैं। जिसमें फंक्शन (Function) Body के सम्‍पूर्ण text को Compiler Main Program में प्रतिस्‍थापित कर सकता हैं।

Inline फंक्शन (Function) को निम्‍न प्रकार से declare कर सकते हैं।

return_type function_name (Argument_list)
{
Function_Statements ;
——————————
——————————-
}

Important Notes


  • Inline फंक्शन (Function) को फंक्शन (Function) calling के पहले declare किया जाता हैं।
  • inline फंक्शन (Function) को overload भी किया जा सकता हैं।

Example of Inline Function

# include <iostream.h>
# include <conio.h>
inline int squre (int a)
{
return (a * a) ;
}

void main ( )
{
clrscr () ;
int x ;
cout << “\n enter any no. =” ;
cin >> x ;
cout << “\n square of input no is =” << square (x);
}

Output :-

Enter any no = 7
Square of input no. is = 49

Inline फंक्शन (Function) की अपनी कुछ सीमायें होती हैं यदि inline फंक्शन (Function) बहुत बड़ा हैं तब compiler उसे अपेक्षित भी कर सकता हैं, क्‍योकि inline keyword के द्वारा Compiler को कोई Command नहीं दी जाती बल्कि उससे request की जाती हैं। Compiler स्थिति को देखते हुये इस Request को स्‍वीकार करता हैं। inline फंक्शन (Function), program execution fast करने के लिये साथ-साथ Memory में बहुत अधिक Space भी लेते हैं।

Default Argument in Inline Function

C++ में फंक्शन (Function) Calling करते समय फंक्शन (Function) को बिना Argument value Fix किये हुये भी Call किया जा सकता हैं। इस स्थिति में फंक्शन (Function) By Default रूप में Arguments में एक Value Fix कर देता हैं जो फंक्शन (Function) के Call के समय दिये गये Argument के जैसे होते हैं। फंक्शन (Function) Argument की default Value को फंक्शन (Function) Declaration के समय Assign किया जाता हैं। Default हमेशा left side से ही assign किये जाते हैं।


निम्‍न उदाहरण default Argument फंक्शन (Function) को प्रदर्शित करता हैं –

#include <iostrem.h>

#include <conio.h>

inline int add (int a, int b = 9, int b = 9, int c =25)
{
Return (a+b+c);
}

void main ()
{
clrscr () ;
Int x = 12 ;
cout << “\n add =” << add (x);
}

Output :-

46

इसमें int add (int a, int b= 9, int c=25) फंक्शन (Function) Prototype Argument b और c के लिये default रूप से क्रमश: 9 और 25 मान दे देता हैं। Main फंक्शन (Function) में जब add फंक्शन (Function) को call किया जाता हैं। उदाहरण के अनुसार add (x) तब add फंक्शन (Function) में x की value a के मिलती हैं। तथा अन्‍य दो Argument की value default रूप से मिल जाती हैं। अत: output 12 + 9 + 25 = add = 46 आता हैं।

Note :- default argument फंक्शन (Function) में argument की value दायें (Right) से बायें (Left) की और Assign की जाती हैं।

inline int add (int a = 12, int b, int c) wrong declaration
inline int add (int a, int b = 6, int c) wrong declaration
inline int add (int a, int b , int c = 6) Correct declaration


error: Content is protected !!