Last modified on April 21st, 2020
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.
Example of inline function in C++
using namespace std;
inline int cube(int x)
int result = x*x*x;
Advantages of Inline Function in C++
- Inline Function speed up your program because it avoids the time waste due to function calling and response back.
- Inline Function increases locality of reference by utilizing instruction cache.
Disadvantages of Inline Function in C++
- 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.
- 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).
- 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
- When performance is needed.
- Use inline function over macros.
- 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.
using namespace std;
inline void operation :: TakeValues()
cout << "Enter first value:";
cin >> n1;
cout << "Enter second value:";
cin >> n2;
inline void operation :: sum()
add = n1+n2;
cout << "Addition of two numbers: " << n1+n2 << "\n";
cout << "Program using inline function\n";
Enter first value: 3
Enter second value: 5
Addition of two numbers: 8
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.