When to use inline function and when not to use it in C/C++?

In C++, there is one good feature called inline function. This kind of functions are like the macros of C or C++. To use inline functions, we have to specify the inline keyword. We can use this type of functions anywhere, but we should follow some guideline.

When inline can be used?

  • Inline functions can be used in the place of macros (#define)

  • For small functions we can use inline functions. It creates faster code and smaller executables.

  • When functions are small and called very often, we can use inline.

When we should avoid the use of inline?

  • We should not use functions that are I/O bound as inline functions.

  • When large code is used in some function, then we should avoid the inline.

  • When recursion is used, inline function may not work properly.

One point we have to keep in mind, that inline is not a command. It is a request. So we are requesting the compiler to use inline functions. If the compiler decides that the current function should not be an inline function, it can convert it into normal function.