Inline Functions in C++
Introduction
The inline keyword instructs the compiler to use the code within the function declaration for every function call.
Inline functions can help your program run quicker by eliminating the overhead associated with function calls. The compiler can optimize functions extended inline in ways conventional functions cannot.
The compiler has complete control over inline code replacement. The compiler, for example, will not inline a function if its address is already in use or if it is too big to inline.
An inline function is declared in the body of a class declaration. Inline functions are available in C++ to minimize function call overhead. A function that expands in line when it is invoked is termed an inline function. The whole code of the inline function is added or replaced at the location of the inline function call when it is invoked. The C++ compiler makes this replacement at the time of compilation. An inline function may increase efficiency if it is tiny.
Use of Inline Functions
When a function call instruction is used in a program, the CPU copies the function's arguments to the stack, caching the memory location of the instruction after the function call and then passing control to the targeted function. The CPU then runs the function's code, saves the return value in a preset register or memory address, and hands control back to the function called the function. If the function's execution time is shorter than the time required to move from the calling function to the called function (callee), this might constitute overhead.
The overhead of the function call is typically negligible compared to how long the function takes to execute for extensive or sophisticated routines. However, the time required to make the function call is sometimes far longer than the time required to actually run the function's code for tiny, frequently called routines. Small functions experience this overhead since their switching time is less than their execution time.
Advantages of Inline Functions
- There is no overhead when calling functions.
- The overhead of pushing and removing variables from the stack when a function is invoked is also avoided.
- Additionally, it avoids the overhead of a function's return call.
- When a function is inlined, the compiler may perform context-specific optimization on the function's body. Regular function calls cannot be optimized in this way. More optimizations may be accomplished by taking into account the fluxes of the caller context and the called context.
- For embedded devices, an inline function could be advantageous (assuming it is tiny), as it can produce less code than the preamble and return functions.
Disadvantages of Inline Functions
- Variables added by inlined functions occupy additional registers. Register resources may become burdened as the number of variables used rises. There is an increase in the number of registers utilized for variables when the inline function body is replaced.
- Due to repeating the same code, using excessive amounts of inline functions will result in a substantial binary executable file size.
- Additionally, excessive inlining might lower your instruction cache hit rate, slowing down the pace at which instructions are fetched from the cache memory and moved to primary memory.
- If someone changes the code inside the inline function, all the calling locations must be recompiled. The compiler would have to replace all the code once more to reflect the changes; otherwise, it will continue to function as before. This could increase compile-time overhead.
- Many embedded systems might not benefit from inline functions because speed is less essential in embedded systems than code size.
- Because inlining could make the binary executable file bigger, it might result in thrashing when using inline functions. The computer's performance suffers when memory thrashing occurs. The inline function is used in the program that follows.
Program Demonstration of Inline Functions
Here is a sample C++ program to demonstrate inline functions,
Code:
#include <iostream>
// Declaration of the inline function
inline int add(int a, int b) {
return a + b;
}
int main() {
int num1, num2;
std::cout << "Enter two numbers: ";
std::cin >> num1 >> num2;
// Calling the inline function
int result = add(num1, num2);
std::cout << "The sum is: " << result << std::endl;
return 0;
}
In the above example, we have created an inline function add with the help of an inline keyword. We have called the function add with two parameters, num1, and num2, and stored the result in a variable named result.
Output: