Inline function in C++

In this tutorial, we are going to learn about inline function in C++. This function is useful because it reduces the execution time of a program. Generally, whenever a function call occurs the program stores the memory address of the next instruction. The control points to the memory location where the function is available. If the function has few instructions, the time for switching becomes larger than the execution time of the instructions. Because of this, efficiency reduces that affects CPU performance. Let’s understand the practical usage of this function with a simple C++ program.

Inline function in C++ with example

The inline function concept in C++ is powerful for programming. Every call requests the compiler to replace the calling statement with the instructions of that function. The ‘inline’ keyword requests the CPU, and it does not command the CPU to do so. If the function is too big, the CPU may not perform inlining. This is used to decrease the execution time of the program. Because during every function call, the control transfer process does not occur. The syntax of an inline function is –

inline return_type function_name(parameters);

The compiler can also ignore the inlining request if such cases occur –

  • The function contains any switch or goto statements.
  • The function return type is different from the ‘void’ data type.
  • There is looping in the instructions like for, while or do-while.
  • If the function contains a static variable or recursion.

Program to illustrate inline function in C++

Let us consider a program to perform some basic tasks on two integer numbers like addition, subtraction, increment, and decrement. We write an inline function when we have to call a function many times during the program execution and it contains few instructions only. The C++ program to illustrate inline function is –

using namespace std;
inline int add(int a,int b)
  return a+b;
inline int subtract(int a,int b)
  return a-b;
inline int increment(int num)
  return ++num;
inline int decrement(int num)
  return --num;
int main()
  int num1,num2;
  cout<<"\nENTER TWO NUMBERS : ";
  cout<<"\nADDITION : "<<add(num1,num2);
  cout<<"\nSUBTRACTION : "<<subtract(num1,num2);
  cout<<"\nINCREMENTING "<<num1<<" : "<<increment(num1);
  cout<<"\nDECREMENTING "<<num2<<" : "<<decrement(num2);
  return 0;

In this program, there are four functions that are made inline using the ‘inline’ keyword. When the compiler encounters an inline function call, it replaces the call with the codes of that function. So, the compiler replaces function calls as follows –

 Inline call           Replacement
add(num1,num2)          num1+num2
subtract(num1,num2)     num1-num2
increment(num1)         ++num1
decrement(num2)         --num2


Remember, if you use many inline functions in a program then the binary file size increases because of duplication.

C++ program output

The output of this simple program to illustrate an inline function in C++ is given below.

[email protected]:~/cpp$ g++ inline.cpp
[email protected]:~/cpp$ ./a.out


DECREMENTING 6 : [email protected]:~/cpp$

If the function execution time is lesser than the control switching time, then overhead occurs. The overhead is not recommended as it degrades the performance and efficiency of CPU. So, in case the function code is large you should use a normal function as the overhead is insignificant while comparing to the execution time of the program.

Also read:

Leave a Reply

Your email address will not be published. Required fields are marked *