What Is Inline Function
What Is Inline Function
};
void Test::fun2() So, what do you find? The machine code
of function fun1 is written at the place of
{
function call inside the main function.
cout << "Non-inline"; When you see the execution of the main
function, fun1 will execute as a part of the
}
main. So, what has happened here? fun1
int main() function has expanded inline. So that is
the meaning of the inline function. The
{
function’s machine code will be pasted or
Test t; copied at the place of the function call. So
fun1 is an inline function.
t.fun1();
t.fun2();
}
Now, how do make a function inline? If
Here we have a class Test which is having
you define a function inside a class
two functions fun1() and fun2(). We have
automatically it is inline. And if you write a
done the body implementation of fun1()
function outside the class then
inside the class and of fun2() outside the
automatically that is a non-inline function.
class using the scope resolution operator.
If you want to make a function non-inline
Here we have two types of functions that
then you must write that function outside
are outside the class and inside the class.
the class. If you want to make a function
inline then you must write that function
inside the class.
Then we have written the main function
and we have created an object t of class
Test. Then we are calling those two
There is one more method to make a
functions i.e. ‘un1() and fun2(). Now if you
function inline and in that case, we just
look at the machine code or object code
need to write inline before the function. {
So, to make fun2 inline, we need to
Test t;
declare the function inside the class as
follows: t.fun1();
t.fun2();
inline void fun2(); }
Output:
We have just added the inline keyword Example to Understand Inline Functions in
before the function return type inside the C++
class. This will become an inline function
and the code will not be separately
generated but it is copied inside the main Where we cannot use Inline Functions?
function. That’s all about inline function.
Remember, making a function inline is
Now let us write the complete program.
only a request to the compiler, not a
command. So, the C++ Compiler can
ignore the request for inlining. The
Example to Understand Inline Functions in
compiler may not perform inlining to the
C++:
functions in the following circumstances
#include <iostream>
using namespace std;
class Test
If the function is a recursive function.
{
If the function contains a loop like for,
public: while, do-while loop.
void fun1() If the function contains static variables.
{ If the function contains a switch or goto
statement.
cout << "Inline\n";
If the function return type is other than
}
void, and the return statement doesn’t
inline void fun2(); exist in the function body.
}; When do we require an Inline Function in
C++?
void Test::fun2()
In C++, we can use the inline function in
{
the following scenarios:
cout << "Non-inline\n";
}
We can use the inline function to improve
int main() the application performance.
It can be used to overcome the macros. If we use many inline functions, then the
binary executable file also becomes large
We can use the inline function outside the
because of the duplication of the same
class so that we can hide the internal
code.
implementation of the function.
The use of too many inline functions can
Advantages of Inline functions in C++:
reduce the instruction cache hit rate, thus
Following are the advantages of using reducing the speed of instruction fetch
Inline Functions in C++. from the cache memory to that of the
primary memory.
It also increases the compile-time
Function call overhead doesn’t occur. In
overhead because if someone changes the
the inline function, we do not require to
code inside the inline function, then the
call a function, so it does not cause any
code needs to be recompiled again to
overhead.
reflect the changes; otherwise, it will
It also saves the overhead of the return execute the old functionality.
statement from a function.
Inline functions might cause thrashing due
It does not require any stack on which we to the increase in the size of the binary
can push or pop the variables as it does executable file. Thrashing in memory
not perform any function calls. So, it saves causes the performance of the computer
the overhead of push/pop variables on to degrade.
the stack when the function is called
Working of inline functions in C++:
An inline function is mainly beneficial for
Working of inline functions in C++
the embedded systems as it yields less
code than a normal function.
Disadvantages of Inline Function in C++: As shown in the above image, we have
created an inline function named
The variables that are created inside the
displayNum() that takes a single integer as
inline function will consume additional
a parameter. We then called the function
registers. After the in-lining function, if the
3 times inside the main() function with
variable number which is going to use the
different arguments. Each time
register increases, then the use of
displayNum() is called, the compiler copies
registers also increases, which may
the code of the function to that call
increase the overhead on register variable
location.
resource utilization. It means that when
the function call is replaced with the inline
function body, the total number of
In the next article, I am going to discuss
variables used by the function also gets
This Pointer in C++ with Examples. Here, in
inserted. So the number of registers going
this article, I try to explain Inline Functions
to be used for the variables will also get
in C++ with Examples and I hope you enjoy
increased. This causes an overhead on
this Inline Functions in C++ with Examples
resource utilization.
article. I would like to have your feedback.
Please post your feedback, question, or
comments about this article.