Inline function in C++ Programming

Inline function is a function which when invoked requests the compiler to replace the calling statement with its body. A keyword inline is added before the function name to make it inline. It is an optimization technique used by the compilers as it saves time in switching between the functions otherwise. Member functions of a class are inline by default even if the keyword inline is not used.

Syntax of Inline Function

inline return_type function_name ([argument list])
{
    body of function
}

Inline function is suitable for small functions only. Incase of large functions, it increases the execution time slowing down the performance. Also, the code size increases reasonably when a large function is called many times since the calling statement is replaced by the body of function every time. So, in case of large functions, the compiler ignores the programmers request to make a function inline even if the keyword inline is used.

When to use inline function

Consider a situation where we have created a very small function to perform some task. When the function is called, the program stores the memory address of next instruction, switches to the address of the function being called. Then the function codes are executed and return value of the function if any is stored. Finally the control is switched back to the memory address of the calling statement stored during function call. When the function is called once or twice, the time required for switching can be ignored. But imagine if the function is called multiple times, it wastes too much memory and time to switch between the functions. And, most of all it also breaks the serial execution of a program consuming considerable amount of execution time.. Hence making the function inline will save both time and memory in these cases.

Examples of Inline function

1. C++ program to print first N natural numbers using inline function

#include <iostream>
#include <conio.h>
using namespace std;
inline void print(int x)
{
    cout<<x<< " ";
}

int main()
{
    int i, N;
    cout<<"C++ Program to print first N natural numbers"<<endl<<endl;
    cout<<"Enter total number of natural numbers:"<<endl;
    cin>>N;
    for(i=1;i<=N;i++)
    {
        print(i);
    }
    getch();
    return 0;
}

Output

C++ Program to print first N natural numbers

Enter total number of natural numbers:
10
1 2 3 4 5 6 7 8 9 10

This program prints the first N natural numbers. In this program, a function print() is made inline. The function receives an integer argument and prints it. The function is called N times in a loop from main function. Every time the calling statement is encountered, it is replaced by the code inside the function.

2. C program to print factorial of first N natural numbers (Ignores inline)

#include <iostream>
#include <conio.h>
using namespace std;
inline void factorial(int x)
{
    int i,f=1,p=1;
    for(i=1;i<=x;i++)
    {
        f=f*i;
    }
    cout<<"Factorial of "<<x<<" is "<<f<<endl;
}

int main()
{
    int i, N;
    cout<<"C++ Program to print factorial of first N natural numbers"<<endl<<endl;
    cout<<"Enter total number of natural numbers:"<<endl;
    cin>>N;
    for(i=1;i<=N;i++)
    {
        factorial(i);
    }
    getch();
    return 0;
}

Output

C++ Program to print factorial of first N natural numbers
Enter total number of natural numbers:
10
Factorial of 1 is 1
Factorial of 2 is 2
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
Factorial of 6 is 720
Factorial of 7 is 5040
Factorial of 8 is 40320
Factorial of 9 is 362880
Factorial of 10 is 3628800

This program calculates the factorial of first N natural numbers. The computation of factorial involves repeated multiplication and loop which makes the function large. So, the compiler ignores the request to make it inline and treats it as normal function.

Note: Try this program in Turbo C++, you will see a warning message saying “Function can’t be expanded inline” but still the program will run normally.