When to use inline function and when not to use it in C/C++?



In C/C++, an inline function is a function where the compiler replaces the function call with the actual code of the function during compilation. So, this makes the program run faster than a normal function call.

Why to Use Inline Function in C/C++?

We should use an inline function in C/C++ when the function is very simple and small size. Also, avoid the regular function call and replace macros with type safety.

Let us understand how an inline function is used for small functions. Suppose we write square(5), the compiler converts it directly to 5*5. This makes the program run a little bit faster because the function call time is saved.

inline int square(int x) {
    return x * x;
}

Note: If you create too many inline functions in the program, then the program size may increase. Secondly, the compiler does not require inlining every time, and if the function is large or has a loop/recursion, then you must ignore it.

Examples of Inline Function

In these examples, we demonstrate the uses of an inline function in a C/C++ program:

C C++
#include <stdio.h>

// Inline function (C99 or later)
inline int square(int x) {
    return x * x;
}

int main() {
    printf("Square of 5 is: %d\n", square(5));
    return 0;
}

Output

The above program produces the following result:

Square of 5 is: 25
#include <iostream>
using namespace std;

// inline function
static inline int square(int x) {
    return x * x;
}

int main() {
    cout << "Square of 5 is: " << square(5) << endl;
    return 0;
}

Output

The above program produces the following result:

Square of 5 is: 25

When NOT to Use Inline Functions in C/C++?

There are several situations where we can avoid the inline functions:

  • When the function contains loops or complex logic: Avoid using inline functions in such cases, as the compiler may skip inlining and it can increase the overall program size.
  • When the function is recursive: Inline should not be used for recursive functions because recursive calls cannot be fully inlined, which may lead to unexpected behavior or confusion.
  • When the function performs I/O operations: Since I/O operations are slow and not suitable for inlining, it's better to avoid using inline functions for such tasks.

Example

In this example, we avoid using the inline keyword before the function due to the use of a loop.

C C++
#include<stdio.h>

// Avoid using inline here due to loop
int sum_upto(int n) {
   int sum = 0;
   for (int i = 1; i <= n; i++) {
       sum += i;
   }
   return sum;
}

int main() {
   printf("Sum up to 100 is: %d\n", sum_upto(121));
   return 0;
}

Output

The above program produces the following result:

Sum up to 100 is: 7381
#include<iostream>
using namespace std;

// Avoid using inline here due to loop
int sum_upto(int n) {
   int sum = 0;
   for (int i = 1; i <= n; i++) {
       sum += i;
   }
   return sum;
}

int main() {
   cout << "Sum up to 100 is: " << sum_upto(121) << endl;
   return 0;
}

Output

The above program produces the following result:

Sum up to 100 is: 7381
Updated on: 2025-07-11T17:48:27+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements