Inline Function in C++

The inline functions help to increase the execution time of a program. The programmer can make a request to the compiler to make the function as inline. Making inline means that compiler can replace those function definitions on the place where they are called.
The compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.
NOTE- This is just a suggestion to the compiler to make the function inline and it totally depends on the compiler to whether make it as inline or not. If a function is big, then the compiler can ignore the “inline” request and treat the function as a normal function.

Inline Function in C++

Example of inline function in C++

Advantages of Inline Function in C++

  1. Inline Function speed up your program because it avoids the time waste due to function calling and response back.
  2. Inline Function increases locality of reference by utilizing instruction cache.

Disadvantages of Inline Function in C++

  1. When we inline the function, it is resolved at compile time. So, we can say that if you change the code of the inline function, then it is necessary to recompile all the programs to make sure that it is updated.Easy notes Inline Function in C++
  2. As mentioned above it increases the executable size, which may cause thrashing in memory. More number of page fault bringing down your program performance. (Thrashing occurs when too many computer processes are competing for memory resources. Thrashing can occur due to a lot of reasons, and one of the common reason is to have a insufficient memory).
  3. Sometimes not useful for example in an embedded system where large executable size is not preferred at all due to memory constraints.

When to use Inline Functions

  1. When performance is needed.
  2. Use inline function over macros.
  3. Prefer to use inline keyword outside the class with the function definition to hide implementation details.

Inline function and classes in C++

We can also define the inline function inside the class. But you must know, all the functions defined inside the class are implicitly inline, so we don’t need to explicitly declare them inline. If we need to explicitly declare an inline function in the class then just we need to declare the function inside the class by using the inline keyword with function.

Output

Enter first value: 3
Enter second value: 5
Addition of two numbers: 8

Video Lecture


inline function in c++ with example pdf | c++ inline member function | inline function in c++ in hindi | c++ inline function in header | advantages and disadvantages of inline function in c++ | non inline function in c++ | inline functions and memory | inline functions and cache | How inline functions speed up the program.

Latest posts by Prof. Fazal Rehman Shamil (see all)

Buy advertisement space on T4Tutorials

For more details email [email protected]