Inline Function in C++

Inline Function is a function that requests the compiler to do inline expansion. In other words, where in normal functions, upon call of a function the execution of the function goes to the definition of the function, in inline function, compiler insert the complete body of the function in every place that the function is called. Inline expansion is used to eliminate the time overhead (excess time) when a function is called. Using the inline function is very simple, one need to just put the keyword ‘inline’ in front of the function definition.
Eg.

#include

using namespace std;

inline void hello()
{
cout<<"hello";
}
int main()
{
hello(); //Call it like a normal function...
return 0;
}

Besides the advantage of Inline function of making the execution of function fast, these are not used in the functions that are tremendously large. This is because when the large functions are made inline, these make the programs inefficient.
Note, rather a warning : the functions with loops should not be made inline as loops increases the size of the function and thus making them inline causes performance overhead.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s