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.
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.
syntax of inline function
inline return-type function-name(parameters)
// Your code
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 speeds 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 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).
- 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 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.
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.