Friday, December 6, 2019

Inline Functions in C++

Introduction to C++ Inline Functions

जब program किसी normal function को execute करता है तो सबसे पहले CPU function call statement के बाद वाले statement का memory address store करता है। ऐसा इसलिए किया जाता है ताकि function के execute होने के बाद इस statement से execution शुरू किया जा सके।
j

इसके बाद compiler arguments को stack में copy करता है। इसके बाद control function को transfer हो जाता है। इसके बाद CPU function code को execute करता है और return value को register में store करता है। इसके बाद control वापस function calling statement को चला जाता है।

जब किसी function का calling time उसके execution time से अधिक होता है तो उसे overhead माना जाता है। Calling time वह time होता है जो CPU को function call और function execution के बीच लगता है।

क्योंकि CPU एक memory location से दूसरी memory location पर switch करता है और उसके बाद function को execute करता है और फिर उसके बाद वापस actual location पर return करता है। इस process में काफी अधिक समय लग जाता है।

बड़े functions जिनको execute होने में अधिक समय लगता है उनके लिए इस time को appropriate माना जा सकता है। लेकिन छोटे functions जो बहुत कम समय में execute हो जाते है उनके लिए एक memory location से दूसरी memory location में switch होने का time waste करना appropriate नहीं होता है।

हालाँकि हो सकता है की अभी ये time आपको महत्वपूर्ण ना लगे लेकिन जब बात बड़े projects की आती है तो execution time reduce करना एक priority होती है।

इस situation में छोटे functions द्वारा waste किये जा रहे function calling time को save करने के लिए C++ आपको एक mechanism provide करती है जिसे inline function कहते है।

Inline function C++ में एक important feature है। जब आप किसी function को inline define करते है तो उस function को execute करने के लिए compiler memory location switch नहीं करता है। इसके बजाय compiler calling statement को function definition से replace कर देता है। ये काम compiler के द्वारा compile time पर किया जाता है।

एक बात ध्यान रखने योग्य है की जब आप किसी function को inline define करते है तो ये compiler के लिए सिर्फ एक request होती है। जरुरी नहीं की compiler इस function को inline करे। Compiler इसे ignore भी कर सकता है।
यदि किसी function में loop होता है, static variables होते है, switch और goto statement होते है या फिर यदि function recursive होता है तो ऐसी situation में compiler function को inline नहीं करता है।

जब आपको program की efficiency बढ़ाने की आवश्यकता हो तो आप inline function को use कर सकते है। Macros के साथ आने वाली problems को eliminate करने के लिए भी inline functions को use किया जाता है।

Advantages of Inline Functions
 
1. क्योंकि function call को function definition से replace कर दिया जाता है इसलिए function call का overhead नहीं होता है। 
2. Argument variables को stack में store करने का overhead भी inline function को use करने से remove हो जाता है।
3. Inline function के use से function के वापस return होने का overhead भी नहीं होता है।

Disadvantages of Inline Functions 

1. Inline function use करने से program बड़ा हो जाता है और executable file की size बढ़ जाती है।
2. क्योंकि function को compile time पर inline किया जाता है, इसलिए यदि आप किसी inline function के code को change करते है तो आपको उन सभी programs को भी recompile करना होगा जो की इस function को use कर रहे है।
3. जब आप inline function को header में use करते है तो इससे आपकी header file बड़ी हो जाती है।
4. जैसा की पहले बताया गया inline function use करने से executable file की size बढ़ जाती है। ऐसा होने से इस executable file के लिए अधिक memory की आवश्यकता पड़ती है। आवश्यकता से कम memory होने पर program की efficiency पर फर्क पड़ता है।

Syntax & Example of Inline Functions

inline return_type function_name(parameter list)
{
     // Function body
}

जैसा की आप ऊपर दिए गए syntax में देख सकते है function को inline बनाने के लिए उसकी normal definition से पूर्व inline keyword का प्रयोग किया गया है।

आइये अब inline function के use को एक उदाहरण के माध्यम से समझने का प्रयास करते है।

#include
using namespace std;


// Inline function add
inline int add(int x, int y)
{
      return x+y;
}

int main()
{
    int a,b;

    cout<<“Please enter first number : “;
    cin>>a;

    cout<<“Please enter second number : “;
    cin>>b;

    cout<
    cout<<“Sum of a & b is :”<

    return 0;  
}

Output :-
                 Please enter first number 
                  9
                 Please enter second number 
                  8
                 Sum of a & b is : 17 

1 comment:

how to add watermark in movavi video editor | movavi watermark | how to ...

What is static keywords in java ? static keywords in java is a main topic of java.