When to use inline function:
The inline function means expansion of a function call that is the function code is integrated (copy and paste) into the caller’s code. It is purely dependent on the compiler whether to ignore or consider the functions as inline function. This freedom of compiler gives the flexibility which itself is a "great advantage to treat the large functions differently from tiny functions. Another plus point is easy to debug but this advantage is totally dependent on the right selection of the compiler options".
There are two things to be considered
If there are many calls to the inline function which makes the code more confusing because it may be present in the source code of C++ language as well as in the header file. It can speed up the execution process if the code is small enough, which should be considered actually small by the compiler. The use of function is most suitable if there is a repeated code. For a small function it is preferable to make the function the candidate for inline. As an example, to find the maximum or minimum of two numbers, the swap two locations etc. It’s a rule of thumb that the break-even point for inline function is three executable lines. It is advisable to keep the inline function as simple as possible.
When not to use inline function:
We know the limitations of macros, which has been explained previously. Now look at the inline function that can prevent us from using it.
A function that contains
- A recursion
- A loop such as while()
- A large size of code
But the biggest advantage of inline function is that it avoids the overhead of calling an actual function. But it is to be noted that with large function code the overhead becomes meaningless or less important. Suppose you have a code for inline function used recursively as demonstrated in program inlinefact.cpp.
- //program name: inlinefact.cpp
- inline int fact(int n);
- int main()
- int n=7;
- cout<<”Factorial of “ <
- int fact(int n)
- return((n==0) ?1 :(n*fact(n-1)));
This type of programming creates a bit of confusion whether the inline function fact() will actually be integrated or not. The answer will be simply no in most compilers. This is just a recommendation to the compiler and the compiler is free to ignore it and consider as a normal function. This is somehow the freedom of the compiler to make a choice by itself.
No one can say that inline functions give more performance than the normal function. It could either be true or false. There is also no guarantee that it prevents the cache from being thrashing, but on the other hand it can. There is no surety that inline function is faster or slower than the normal function. There is no guarantee that the compiler will consider the function code as an inline code. As discussed before, it is up to the compiler to consider the function the way it should be. There is no guarantee that the speed will be relatively faster than the normal functions. No one can recommend that use inline function of do not use. At the end one must follow the rule of thumb using inline function.