Inline functions Advantages

  1. Function call overhead doesn’t occur. 
  2. It also saves the overhead of push/pop variables on the stack when a function is called. 
  3. It also saves the overhead of a return call from a function. 
  4. When you inline a function, you may enable the compiler to perform context-specific optimization on the body of the function. Such optimizations are not possible for normal function calls. Other optimizations can be obtained by considering the flows of the calling context and the called context. 
  5. An inline function may be useful (if it is small) for embedded systems because inline can yield less code than the function called preamble and return. 

Inline Functions in C++

C++ provides inline functions to reduce the function call overhead. An inline function is a function that is expanded in line when it is called. When the inline function is called whole code of the inline function gets inserted or substituted at the point of the inline function call. This substitution is performed by the C++ compiler at compile time. An inline function may increase efficiency if it is small.

Syntax:

inline return-type function-name(parameters)
{
    // function code
}  

 

Remember, inlining is only a request to the compiler, not a command. The compiler can ignore the request for inlining. 

The compiler may not perform inlining in such circumstances as: 

  1. If a function contains a loop. (for, while and do-while
  2. If a function contains static variables. 
  3. If a function is recursive. 
  4. If a function return type is other than void, and the return statement doesn’t exist in a function body. 
  5. If a function contains a switch or goto statement. 

Why Inline Functions are Used?

When the program executes the function call instruction the CPU stores the memory address of the instruction following the function call, copies the arguments of the function on the stack, and finally transfers control to the specified function. The CPU then executes the function code, stores the function return value in a predefined memory location/register, and returns control to the calling function. This can become overhead if the execution time of the function is less than the switching time from the caller function to called function (callee). 

For functions that are large and/or perform complex tasks, the overhead of the function call is usually insignificant compared to the amount of time the function takes to run. However, for small, commonly-used functions, the time needed to make the function call is often a lot more than the time needed to actually execute the function’s code. This overhead occurs for small functions because the execution time of a small function is less than the switching time.

Similar Reads

Inline functions Advantages:

Function call overhead doesn’t occur.  It also saves the overhead of push/pop variables on the stack when a function is called.  It also saves the overhead of a return call from a function.  When you inline a function, you may enable the compiler to perform context-specific optimization on the body of the function. Such optimizations are not possible for normal function calls. Other optimizations can be obtained by considering the flows of the calling context and the called context.  An inline function may be useful (if it is small) for embedded systems because inline can yield less code than the function called preamble and return....

Inline function Disadvantages:

The added variables from the inlined function consume additional registers, After the in-lining function if the variable number which is going to use the register increases then they may create overhead on register variable resource utilization. This means that when the inline function body is substituted at the point of the function call, the total number of variables used by the function also gets inserted. So the number of registers going to be used for the variables will also get increased. So if after function inlining variable numbers increase drastically then it would surely cause overhead on register utilization.  If you use too many inline functions then the size of the binary executable file will be large, because of the duplication of the same code.  Too much inlining can also reduce your instruction cache hit rate, thus reducing the speed of instruction fetch from that of cache memory to that of primary memory.  The inline function may increase compile time overhead if someone changes the code inside the inline function then all the calling location has to be recompiled because the compiler would be required to replace all the code once again to reflect the changes, otherwise it will continue with old functionality.  Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed.  Inline functions might cause thrashing because inlining might increase the size of the binary executable file. Thrashing in memory causes the performance of the computer to degrade. The following program demonstrates the use of the inline function....

Inline function and classes

...

What is wrong with the macro?

It is also possible to define the inline function inside the class. In fact, all the functions defined inside the class are implicitly inline. Thus, all the restrictions of inline functions are also applied here. If you need to explicitly declare an inline function in the class then just declare the function inside the class and define it outside the class using the inline keyword....