How To Implement Inline Function in C++?

Last updated on Sep 26,2020 26.2K Views

How To Implement Inline Function in C++?

This article will introduce you to an important concept in programming domain,that is Inline Function in C++ and follow it up with a practical demonstration. Following Pointers will be covered in this article

Let us get started with this article on Inline function in C++

What is an Inline function in C++?

One of the major objectives of using functions in a program is to save memory space, which becomes appreciable when a function is likely to be called many times. However, every time a function is called, it takes a lot of extra time in executing tasks such as jumping to the calling function. When a function is small, a substantial percentage of execution time may be spent in such overheads and sometimes maybe the time taken for jumping to the calling function will be greater than the time taken to execute that function.

One solution to this problem is to use macro definitions, commonly known as macros. Preprocessor macros are popular in C, but the major drawback with macros is that they are not really functions and therefore, the usual error checking process does not occur during compilation.

C++ has a different solution to this problem. To eliminate the time of calls to small functions, C++ proposes a new function called inline function. An inline function is a function that is expanded in line when it is invoked thus saving time. The compiler replaces the function call with the corresponding function code that reduces the overhead of function calls.

We should note that inlining is only a request to the compiler, not a command. The compiler can ignore and skip the request for inlining. The compiler may not perform inlining in the following circumstances:

  • If a function contains a loop. (for, while, do-while)
  • If a function is recursive.
  • If a function contains static variables.
  • If a function contains a switch command or goto statement.
  • For a function not returning values, if a return statement exists. 

For an inline function, declaration and definition must be done together.

inline function-header


#include <iostream> 
using namespace std; 
inline int cube(int s) 
return s*s*s; 
inline int inc(int a) 
return ++a; 
int main() 
int a = 11;
cout << "The cube of 3 is: " << cube(3) << "n"; 
cout << "Incrementing a " << inc(a) << "n";
return 0; 


Output- Inline function in C++- Edureka


It is a simple example which shows two inline functions declared using the inline keyword as a prefix.

Moving on with this article on Inline function in C++

When to use Inline function?

We can use Inline function as per our needs. Some useful recommendation are mentioned below-

  • We can use the inline function when performance is needed.
  • We can use the inline function over macros.
  • We prefer to use the inline keyword outside the class with the function definition to hide implementation details of the function.

Moving on with this article on Inline function in C++

Points to be remembered while using Inline functions

  • We must keep inline functions small, small inline functions have better efficiency and good results.
  • Inline functions do increase efficiency, but we should not turn all the functions inline. Because if we make large functions inline, it may lead to code bloat and might end up decreasing the efficiency.
  • It is recommended to define large functions outside the definition of a  class using scope resolution:: operator, because if we define such functions inside a class definition, then they might become inline automatically and again affecting the efficiency of our code.

Moving on with this article on Inline function in C++

Advantages of Inline function

  • Function call overhead doesn’t occur.
  • It saves the overhead of a return call from a function.
  • It saves the overhead of push/pop variables on the stack when the function is called.
  • When we use the inline function it may enable the compiler to perform context-specific optimization on the function body, such optimizations are not possible for normal function calls.
  • It increases the locality of reference by utilizing the instruction cache.
  • An inline function may be useful for embedded systems because inline can yield less code than the function call preamble and return.

Moving on with this article on Inline function in C++

Limitations of Inline functions

  • Large Inline functions cause Cache misses and affect efficiency negatively.
  • Compilation overhead of copying the function body everywhere in the code at the time of compilation which is negligible in the case of small programs, but it may make a big difference in large codebases.

  • If we require address of the function in a program, the compiler cannot perform inlining on such functions. Because for providing the address to a function, the compiler will have to allocate storage to it. But inline functions don’t get storage, they are kept in Symbol table.
  • Inline functions might cause thrashing because it might increase the size of the binary executable file. Thrashing in memory causes the performance of the computer to degrade and it also affects the efficiency of our code.
  •  The inline function may increase compile time overhead if someone tried to changes the code inside the inline function then all the calling location has to be recompiled again because the compiler would require to replace all the code once again to reflect the changes, otherwise it will continue with old functionality without any change.

Thus we have come to an end of this article on ‘Inline Function in C++’. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.


Join the discussion

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.