Saturday 1 January 2022

Q6. What is Inline Function? How is it different from Macro? Explain with examples. इनलाइन फंक्शन क्या है? यह मैक्रो से किस प्रकार भिन्न है? उदाहरण सहित स्पष्ट कीजिए।


Definition of Inline

An inline function looks like a regular function but, is preceded by the keyword “inline“. Inline functions are short length functions which are expanded at the point of its invocation, instead of being called. Let’s understand inline functions with an example.

इनलाइन की परिभाषा

एक इनलाइन फ़ंक्शन एक नियमित फ़ंक्शन की तरह दिखता है, इसके लिए "इनलाइन" शब्‍द का प्रयोग पहले किया जाता है। इनलाइन फ़ंक्शंस छोटी लंबाई के फ़ंक्शन होते हैं जिन्हें कॉल किए जाने के बजाय इसके आह्वान के बिंदु पर विस्तारित किया जाता है। आइए एक उदाहरण के साथ इनलाइन फ़ंक्शंस को समझते हैं।

#include <iostream>

using namespace std;

class example{

                int a, b;

                public:

                inline void initialize( int x, int y){ a=x; b=y }

                void display(){ cout<< a <<" "<<b << \n; } //automatic inline

};

int main( )

{

                example e;

                e. initialize(10, 20);

                e.display();

}

In above program, I declared and defined, the function initialize ( ), as an inline function in the class “example”.

 

Difference between Inline and Macro in C++:


S.NO

Inline

Macro

1.

An inline function is defined by the inline keyword.

इनलाइन फ़ंक्शन को इनलाइन कीवर्ड द्वारा परिभाषित किया गया है।

Whereas the macros are defined by the #define keyword.

जबकि मैक्रोज़ को #define कीवर्ड द्वारा परिभाषित किया जाता है।

2.

Through inline function, the class’s data members can be accessed.

इनलाइन फ़ंक्शन के माध्यम से, Class के डेटा Member को प्रयोग किया जा सकता है।

Whereas macro can’t access the class’s data members.

जबकि मैक्रो द्वारा क्‍लास के डाटा सदस्‍यों का इस्‍तेमाल नहीं किया जा सकता है।

3.

In the case of inline function, the program can be easily debugged.

इनलाइन फ़ंक्शन के मामले में, प्रोग्राम को आसानी से डीबग(समस्‍याओ को ढूंढना) किया जा सकता है।

Whereas in the case of macros, the program can’t be easily debugged.

जबकि मैक्रोज़ के मामले में, प्रोग्राम को आसानी से डिबग(समस्‍याओ को ढूंढना) नहीं किया जा सकता है।

4.

In the case of inline, the arguments are evaluated only once.

इनलाइन के मामले में, Arguments को केवल एक बार ही मूल्‍यांकन किया जाता है।

Whereas in the case of macro, the arguments are evaluated every time whenever macro is used in the program.

जबकि मैक्रो के मामले में, जब भी प्रोग्राम में मैक्रो का उपयोग किया जाता है, तो तर्कों का मूल्यांकन हर बार किया जाता है।

5.

In C++, inline may be defined either inside the class or outside the class.

सी ++ में, इनलाइन को कक्षा के अंदर या कक्षा के बाहर परिभाषित किया जा सकता है।

Whereas the macro is all the time defined at the beginning of the program.

जबकि मैक्रो को प्रोग्राम की शुरुआत में हमेशा परिभाषित किया जाता है।

6.

In C++, inside the class, the short length functions are automatically made the inline functions.

सी ++ में, कक्षा के अंदर, छोटी लंबाई के कार्यों को स्वचालित रूप से इनलाइन फ़ंक्शन बना दिया जाता है।

While the macro is specifically defined.

जबकि मैक्रो को विशेष रूप से परिभाषित किया गया है।

7.

Inline is not as widely used as macros.

इनलाइन का व्यापक रूप से मैक्रोज़ के रूप में उपयोग नहीं किया जाता है।

While the macro is widely used.

जबकि मैक्रो का व्यापक रूप से उपयोग किया जाता है।

8.

Inline is not used in competitive programming.

प्रतिस्पर्धी प्रोग्रामिंग में इनलाइन का उपयोग नहीं किया जाता है।

While the macro is very much used in competitive programming.

जबकि प्रतिस्पर्धी प्रोग्रामिंग में मैक्रो का बहुत अधिक उपयोग किया जाता है।

9.

Inline function is terminated by the curly brace at the end.

अंत में { कोष्‍ठक द्वारा इनलाइन फ़ंक्शन को समाप्त किया जाता है।

While the macro is not terminated by any symbol, it is terminated by a new line.

जबकि मैक्रो को किसी प्रतीक द्वारा समाप्त नहीं किया जाता है, इसे एक नई लाइन में आकर समाप्त किया जाता है।

 


No comments:

Post a Comment