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.

Example

In this example, we demonstrate the basic usage 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
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

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

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

i. When function has loops or complex logic: Please avoid inline as the compiler skips inline and increases the program size.

ii. When function has recursion: In this case, never use inline because a recursive call cannot be completely inlined and may lead to program confusion.

iii. When a function performs I/O operations: As the input-output processing is slow. So, it's better not to use it.

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-01T15:09:18+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements