Inline Function in C++ with Example

By
Last updated:

We mentioned that inline function save memory space because all the calls to the function cause the same code to be executed; the function body need not be duplicated in memory. When the compiler sees a function call, it normally generates a jump to the function. At the end of the function it jumps back to the instruction following the call, as shown in Figure earlier in this post.

While this sequence of events may save memory space, it takes some extra time. There must be an instruction for the jump to the function (actually the assembly language instruction CALL or something similar), instructions for saving registers, instructions for pushing arguments onto the stack in the calling program and removing them from the stack in the function (if there are arguments), instructions for restoring registers, and an instruction to return to the calling program. The return value (if any) must also be dealt with. All these instructions slow down the program.

To save execution time in short functions, you may elect to put the code in the function body directly inline with the code in the calling program. That is, each time there’s a function call in the source file, the actual code from the function is inserted, instead of a jump to the function. The difference between a function and inline code is shown in Figure.

Long sections of repeated code are generally better off as normal functions: The savings in memory space is worth the comparatively small sacrifice in execution speed. But making a short section of code into an ordinary function may result in little savings in memory space, while imposing just as much time penalty as a larger function. In fact, if a function is very short, the instructions necessary to call it may take up as much space as the instructions within the function body, so that there is not only a time penalty but a space penalty as well.

In such cases you could simply repeat the necessary code in your program, inserting the same group of statements wherever it was needed. The trouble with repeatedly inserting the same code is that you lose the benefits of program organization and clarity that come with using functions. The program may run faster and take less space, but the listing is longer and more complex.

The solution to this quandary is the inline function. This kind of function is written like a normal function in the source file but compiles into inline code instead of into a function. The source file remains well organized and easy to read, since the function is shown as a separate entity. However, when the program is compiled, the function body is actually inserted into the program wherever a function call occurs.

Functions that are very short, say one or two statements, are candidates to be inlined. Here’s INLINE, a variation on the CONVERT2 program. It inlines the lbstokg() function.

// inliner.cpp
// demonstrates inline functions
#include <iostream>
using namespace std;
// lbstokg()
// converts pounds to kilograms
inline float lbstokg(float pounds)
{
return 0.453592 * pounds;
}
//--------------------------------------------------------------
int main()
{
float lbs;
cout << “\nEnter your weight in pounds: “;
cin >> lbs;
cout << “Your weight in kilograms is “ << lbstokg(lbs)
<< endl;
return 0;
}

It’s easy to make a function inline: All you need is the keyword inline in the function definition:

inline float lbstokg(float pounds)

You should be aware that the inline keyword is actually just a request to the compiler.

Sometimes the compiler will ignore the request and compile the function as a normal function.

It might decide the function is too long to be inline, for instance.

(C programmers should note that inline functions largely take the place of #define macros in C. They serve the same purpose but provide better type checking and do not need special care with parentheses, as macros do.)

Read More Topics
Definition of function in C
User defined function
Recursion in C++
Overloaded functions in C++
Relational operators in C++

Santhakumar Raja

Hello The goal of this blog is to keep students informed about developments in the field of education. encourages pupils to improve as writers and readers.

For Feedback - techactive6@gmail.com

Leave a Comment