What are Inline Functions in C++-Advantages and disadvantages

What is meant by inline fucntion in C++?

Inline function is a kind of optimization method which is made use by compilers.

You need to pre-append the keyword, ‘inline’ in front of function declaration for making the function as inline.

The inline methods will provide instructions for compiler for inserting the function body wherever the function is used inside the program.

Advantages of inline functions:

  1. Inline methods never involve any overhead of function call.
  2. Overhead of stack operations like push or pop during function calling is avoided.
  3. Inline functions also reduce the overhead of function return calls.
  4. By using the instruction caches, locality of reference is increased.
  5. Intra-procedural optimization shall also be specified after in-lining the compiler. This is the most essential advantage as in this way the compiler gets focused on elimination of dead coding and can stress more on elimination of induction variable, branch prediction, etc.

Disadvantages of inline functions:

  1. Inline function increases method size which never fits within cache and this causes many cache miss.
  2. If the number of register variables increases after in-lining the functions, it creates resource utilization overhead.
  3. Compilation overhead will be created by inline functions.
  4. When the inline functions are used in header files, the size of these header files increases and also made unreadable.
  5. Inline functions use huge code size resulting in memory thrashing. Page faults are also caused which degrades the performance of your program.
  6. Inline functions are never helpful with embedded systems as binary system is not compatible with inline functions because of memory constraints.