Question : I want a program on INLINE function and demonstrate the program with INLINE function and without INLINE function.Because i want to know clearly the difference between INLINE FUNCTION AND FUNCTION
Inline functions get copied to every locations that make a call to the function. That is the entire function body (all statements) are inlined into source code at every calling location.
Inline functions should only be used if the body of the function is only a few lines of code.
In C++ the inlines are like macros. they also do Code Expansion.
There are two problems with the use of preprocessor macros in C++. The first is: a macro looks like a function call,but doesn?t always act like one. This can bury difficult-to-find bugs. The second is : The preprocessor has no permission to access class member data. This means preprocessormacros cannot be used as class member functions.
C++implements the macro as inline function, which is a true function in every sense. Any behavior you expect from an ordinary function, you get from an inline function. The only difference is that an inline function is expanded in place, like a preprocessor macro, so the overhead of the function call is eliminated.
#include<iostream> using namespace std; #define Add(x,y) {x+y;} inline int add(int,int){ int c=x+y; return c;}
int main() { int x=4,y=6; cout<<"This is macro"<<Add(10,12); cout<<"This is inline"<<add(10,12); cout<<"This is macro"<<Add(x++,++y); cout<<"This is inline"<<Add(x++,++y); return 0; } try it and test the result. if you are running this in linux.Then check after preprocessing. use the command CC -E filename.c
In C++ the inlines are like macros. they also do Code Expansion.
There are two problems with the use of preprocessor macros in C++. The first is: a macro looks like a function call,but doesn?t always act like one. This can bury difficult-to-find bugs. The second is : The preprocessor has no permission to access class member data. This means preprocessormacros cannot be used as class member functions.
C++implements the macro as inline function, which is a true function in every sense. Any behavior you expect from an ordinary function, you get from an inline function. The only difference is that an inline function is expanded in place, like a preprocessor macro, so the overhead of the function call is eliminated.
#include<iostream> using namespace std; #define Add(x,y) {x+y;} inline int add(int,int){ int c=x+y; return c;}
int main() { int x=4,y=6; cout<<"This is macro"<<Add(10,12); cout<<"This is inline"<<add(10,12); cout<<"This is macro"<<Add(x++,++y); cout<<"This is inline"<<Add(x++,++y); return 0; } try it and test the result. if you are running this in linux.Then check after preprocessing. use the command CC -E filename.c
In C++ the inlines are like macros. they also do Code Expansion.
There are two problems with the use of preprocessor macros in C++. The first is: a macro looks like a function call,but doesn?t always act like one. This can bury difficult-to-find bugs. The second is : The preprocessor has no permission to access class member data. This means preprocessormacros cannot be used as class member functions.
C++implements the macro as inline function, which is a true function in every sense. Any behavior you expect from an ordinary function, you get from an inline function. The only difference is that an inline function is expanded in place, like a preprocessor macro, so the overhead of the function call is eliminated.
#include<iostream> using namespace std; #define Add(x,y) {x+y;} inline int add(int,int){ int c=x+y; return c;}
int main() { int x=4,y=6; cout<<"This is macro"<<Add(10,12); cout<<"This is inline"<<add(10,12); cout<<"This is macro"<<Add(x++,++y); cout<<"This is inline"<<Add(x++,++y); return 0; } try it and test the result. if you are running this in linux.Then check after preprocessing. use the command CC -E filename.c
the inline function is not command like a function.It is just like a request to decrease the comprehensibility of the code. the syntax is: inline function-header name { arguments }
#include<iostream.h> class Demo { public: Demo(){}
void display() { cout<<"This is an inline function"<<endl; } }; void display() { cout<<"This is a regular function"<<endl; }
void main() { display(); Demo().display(); }
observe that a function is inline if it is a member function to a class and is written within the class.If a function isn't a member of a class it is said to be a regular function. In the above program,"Demo().display()" calls the inline display() function and "display()" calls the regular display() function.
Difference between INLINE function and a normal function is we can find it in execution of a program containing a normal fn and INLINE fn .when we execute this pgm when you call a inline function the entire code written in the function will be substituted there..where as when you call a normal function the control jumps to the line of that function and it executes. NOTE:the code of the INLINE function should be very small SYNTAX: inline return type fn() { statments