Inline Function in C++

The inline functions help to decrease the execution time of a program. The programmer can make a request to the compiler to make the function inline. Making inline means that the compiler can replace those function definitions in the place where they are called.
The compiler replaces the definition of inline functions at compile time instead of referring function definition at runtime.

inline means that compiler can replace those function definitions on the place where function called
inline means that compiler can replace those function definitions on the place where function called

What is Compile-time?

The time required for a source code to convert into an executable code is called Compile time.

What is Run time?

The time at which the executable code is started running is called run time.

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 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++

syntax of inline function

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

Example of inline function in C++

Advantages of Inline Function in C++

  1. Inline Function speeds 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 faults bring 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 reasons is to have 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 functions 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

FAQ

Inline functions can increase the code size of our program and add stress to the instruction cache.

Inline Function MCQs

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.

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.