Inline Functions in C++

As we know, when we call a function from the main program, the control jumps to the function and then comes back to the calling program when execution of function is completed. This process, although looks simple, but involves a lot of background processes as of passing of parameters to the function, allocating of storage for function’s local variables, storing the current variables, etc.

C++ provides a convenient way of omitting all these processes by simply declaring the function to be inline.

When we declare a function inline, we tell the compiler to replace each call to such function(declared inline) with the explicit code of the function. To declare a function inline all we need to do is to add the word inline before the definition of the function as:

inline data-type function_name(type 1 arg 1, type 2 arg 2, ..., type n arg n);

Also, in the function-prototype of the inline function, we are required to add the word inline before the prototype as:

inline data-type function_name(type 1 arg 1, type 2 arg 2, ..., type n arg n);

Now, let us illustrate inline function with a simple example as follows:

Consider the following program where an inline function cube() is defined before main (hence it will not require function prototype) and then is called in main:

#include<iostream.h>

inline int cube(int x)
{
 //returns cube of x
 return x*x*x;
}

int main()
{
 cout << cube(4) <<endl;
}

The above program will actually be compiled as follows:

int main()
{
 cout << (4)*(4)*(4) << endl;
}

Limitation of using inline functions:
Use of the inline functions can also cause negative side effects. For example, inlining a 40-line function that is called in 26 different locations would add at least 1000 lines of unnoticed source code to your program. Inlined functions can also limit the portability of your code across platforms.

You may also like...

4 Responses

  1. nitish kumar says:

    Tanks ,its very helpful for me.

  2. nitish kumar says:

    Sir why weuse function inline

    • Prashant N. says:

      As we know, when we call a function from the main program, the control jumps to the function and then comes back to the calling program when execution of function is completed. This process, although looks simple, but involves a lot of background processes as of passing of parameters to the function, allocating of storage for function’s local variables, storing the current variables, etc.

      C++ provides a convenient way of omitting all these processes by simply declaring the function to be inline. When we declare a function inline, we tell the compiler to replace each call to such function(declared inline) with the explicit code of the function.

      So basically, if we use inline, it will minimize the number of function calls and thus minimizing the number of background processes

      Hope this answers your query

  3. Mark says:

    I’ve learn some good stuff here. Certainly worth bookmarking for revisiting.
    I surprise how a lot attempt you put to create the sort of fantastic informative website.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: