Inline Functions in C++

Suraj P Oct 12, 2023
  1. Why inline Functions Are Used in C++
  2. Implement inline Functions in C++
  3. Implement constructor and destructor as inline Functions in C++
  4. Advantages and Disadvantages of Implementing inline Functions in C++
Inline Functions in C++

This article will discuss inline functions in C++, how to implement them, and the advantages and disadvantages of using them.

Why inline Functions Are Used in C++

Whenever we execute a program, the CPU stores the instruction’s memory address following the function call; it copies the arguments onto the stack and transfers the control to the specified function.

Then the CPU executes the code, returns the value at a specific memory location, and returns to the called function again. It might create an extra overhead if most of the time is spent on switching rather than executing the function.

This overhead is insignificant in large functions that perform complex CPU-intensive tasks since most of the time is spent only on execution.

However, this overhead becomes significant when we have many small functions that only perform basic tasks; most of the time is spent switching, and little to no time is spent on execution.

So, the concept inline can be handy when we have many small functions to save switching time and increase efficiency.

Whenever any function is declared as inline in the code at compile-time, instead of assigning an address at the function call, it copies the whole function code and puts it in that place.

Syntax:

inline return_type function_name(arguments)
{
...
}

An inline function is only a request to the compiler, and so the compiler can ignore it if:

  1. the function has static variables;
  2. the function has goto or switch statements;
  3. the function is recursive;
  4. the function has loops.

The concept of inline is generally used with classes.

Implement inline Functions in C++

Example code:

#include <iostream>
using namespace std;

class myClass {
  int a, b;

 public:
  void sum();
};

inline void myClass ::sum() {
  cout << "Enter first value:";
  cin >> a;
  cout << "Enter second value:";
  cin >> b;
  cout << "Sum of two numbers is " << a + b << endl;
}

int main() {
  cout << "Program using inline function\n";
  myClass obj;
  obj.sum();
}

In the code above, we declared the function as inline while defining it because it’s a good programming practice to write the actual inline function definition outside the class than inside the class.

However, a function definition in a class is an inline function definition by default, even without using the inline keyword.

Output:

Program using inline function
Enter first value:12
Enter second value:13
Sum of two numbers is 25

Implement constructor and destructor as inline Functions in C++

Using the above example and making inline functions outside the class while defining it, we can even make constructor and destructor as inline.

Example code:

#include <iostream>
using namespace std;

class myClass {
  int a, b;

 public:
  myClass();
  ~myClass();
  void sum();
};

inline myClass::myClass() {
  a = 100;
  b = 200;
}

inline myClass::~myClass() { cout << "destroying the object\n"; }

inline void myClass ::sum() {
  cout << "Sum of two numbers is " << a + b << endl;
}

int main() {
  cout << "Program using inline function\n";
  myClass obj;
  obj.sum();
}

Output:

Program using inline function
Sum of two numbers is 300
destroying the object

Advantages and Disadvantages of Implementing inline Functions in C++

Now, let’s look at some of the advantages of implementing inline functions:

  1. Overhead of the function call is reduced.
  2. It saves the overhead of a return call from a function.
  3. Saves overhead of pushing and popping variables on the stack.

Though useful, it has some disadvantages as well:

  1. The size of the binary executable file might become large if we use too many inline functions because duplication of the same code will happen here.
  2. Too many inline functions can reduce the cache hit rate for the instructions, which affects the speed of instruction fetch from cache memory to primary memory.
  3. In embedded systems where code size is more important than speed, inline functions won’t be helpful.
  4. Thrashing could occur, which will degrade the memory performance of the computer.
Author: Suraj P
Suraj P avatar Suraj P avatar

A technophile and a Big Data developer by passion. Loves developing advance C++ and Java applications in free time works as SME at Chegg where I help students with there doubts and assignments in the field of Computer Science.

LinkedIn GitHub

Related Article - C++ Function