Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

What is Inline Function?

then sometimes the time taken required


for moving to the calling function will be
Normally, a function call transfers the
greater than the time taken required to
control from the calling program to the
execute that function. The solution to this
called function. This process of jumping to
problem is to use Inline Functions.
the function, saving registers, passing a
value to the argument, and returning the
value to the calling function takes extra
In the case of function calls, the time for
time and memory.
calling small functions is huge, so to
overcome such a problem, C++ Introduced
a new feature called Inline Function.
An inline function is a function that is
When the function is called inside the
expanded inline when it is invoked, thus
main() method, it is expanded with its
saving time. The compiler replaces the
definition thus saving time.
function call with the corresponding
function code, which reduces the
overhead of function calls.

C++ provides an inline function to reduce


When an inline function is called, the the function call overhead. An inline
compiler replaces all the calling function is a function that is expanded in
statements with the function definition. line when it is called. When the inline
Every time an inline function is called, the function is called whole code of the inline
compiler generates a copy of the function gets inserted or substituted at
function’s code, in place, to avoid the the point of the inline function call. This
function call. substitution is performed by the C++
compiler at compile time. The inline
function may increase efficiency if it is
small. The syntax for defining the function
inline is:

Why inline functions are used and what is


Why inline functions are used and what is
the purpose of inline functions?
the purpose of inline functions?
Now, let us understand why and when we
should use inline functions in C++. The Example to Understand Inline Functions in
main use of the inline function in C++ is to C++:
save memory space. Whenever a function
The functions which expand in the same
is called, then it takes a lot of time to
line where they are called are Inline
execute the tasks, such as moving to the
Functions in C++. There is no separate
calling function. If the length of the
block for that function. For a better
function code is very small and if takes a
understanding of the Inline Function,
very small amount of time to execute,
please have a look at the below code.
generated for this code then how does it
look like? Let us see.
class Test
{
Inline Functions in C++ with Examples
public:
void fun1 ()
The above image shows the object code
{
for our program. Let us assume that
cout << "Inline"; dotted lines are the machine instructions.
Then there is a call for fun1 inside the
}
main function. And for fun2 separate
void fun2 (); machine code will be generated.

};
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.

You might also like