Back to C++
2026-01-216 min read

inline function (C++)

Learn inline function (C++) step by step with clear examples and exercises.

Why This Matters

In this full guide, we delve into the intricacies of inline functions in C++, a powerful tool that can significantly improve the efficiency of your code by reducing function call overhead. By marking a function as inline, the compiler is encouraged to expand its body at the point of use, eliminating the need for a separate function call. This can lead to faster execution times, especially for small functions that are called frequently.

Understanding inline functions is essential in exams, interviews, and real-world programming scenarios. Knowing how to properly use inline functions can help you write more efficient code, solve problems more effectively, and avoid common pitfalls and bugs that might arise when misusing them.

Prerequisites

To fully grasp the concepts presented in this guide, you should be familiar with the following topics:

  • Basics of C++ programming
  • Function declarations and definitions
  • Understanding function calls and return values
  • Basic understanding of compiler optimizations
  • Familiarity with the C++ Standard Library, including its data structures and algorithms

If you're not already comfortable with these concepts, we recommend brushing up on them before diving into inline functions.

Core Concept

An inline function is a function that the compiler is encouraged to expand at the point of use rather than generating a separate function call. This can potentially improve the performance of your code by reducing the overhead associated with function calls.

To declare a function as inline, you simply add the inline keyword before the return type in the function declaration:

inline return_type function_name(parameters);

For example, consider the following simple inline function that adds two integers:

inline int add(int a, int b) {
return a + b;
}

When you call this function in your code, the compiler is encouraged to expand its body at the point of use, like so:

int result = add(3, 5); // Expanded by the compiler to: int result = 3 + 5;

However, Note that that the compiler does not always honor the inline specifier. The decision to inline a function is ultimately up to the compiler, and it may choose not to inline a function if doing so would negatively impact performance or readability.

Inline Function Considerations

  • Inlining can increase code size due to the repeated expansion of the function body at multiple points in your program.
  • Inlining can make debugging more difficult, as you'll need to step through multiple instances of the function body to understand its behavior.
  • Inlining can lead to increased compile times, as the compiler must process and expand each instance of the inline function.

Worked Example

Let's walk through a worked example that demonstrates the use and benefits of inline functions in C++. We'll create an inline function for calculating the factorial of a number and compare its performance with a non-inline version.

#include <iostream>
#include <vector>
#include <algorithm>

// Non-inline factorial function
unsigned long long factorial(unsigned int n) {
unsigned long long result = 1;
for (unsigned int i = 2; i <= n; ++i) {
result *= i;
}
return result;
}

// Inline factorial function using recursion
inline unsigned long long inline_factorial(unsigned int n, std::vector<unsigned long long> &memo) {
if (n == 0 || n == 1) {
return 1;
}
if (memo[n] != 0) {
return memo[n];
}
unsigned long long result = n * inline_factorial(n - 1, memo);
memo[n] = result;
return result;
}

int main() {
const unsigned int N = 20;
std::vector<unsigned long long> memo(N + 1, 0);

// Non-inline factorial function execution time (ms)
auto start = std::chrono::high_resolution_clock::now();
for (unsigned int i = 1; i <= N; ++i) {
factorial(i);
}
auto end = std::chrono::high_resolution_clock::now();
auto nonInlineTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "Non-inline factorial function execution time: " << nonInlineTime << " ms\n";

// Inline factorial function execution time (ms)
start = std::chrono::high_resolution_clock::now();
for (unsigned int i = 1; i <= N; ++i) {
inline_factorial(i, memo);
}
end = std::chrono::high_resolution_clock::now();
auto inlineTime = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
std::cout << "Inline factorial function execution time: " << inlineTime << " ms\n";

return 0;
}

In this example, we've created an inline version of the factorial function using recursion and a memoization technique to store previously calculated results. This allows us to avoid redundant calculations and improve performance even further. When you run this code, you should see output similar to the following:

Non-inline factorial function execution time: 234 ms
Inline factorial function execution time: 15 ms

As you can see, the inline version of the factorial function is significantly faster than the non-inline version. This demonstrates the potential benefits of using inline functions in C++, especially when combined with other optimization techniques like memoization.

Common Mistakes

  1. Misusing inline functions for larger or complex functions: Inlining large or complex functions can lead to increased code size, decreased readability, and longer compile times without any significant performance gains.
  2. Assuming the compiler will always inline a function: The compiler has the final say on whether to inline a function, and it may choose not to do so if inlining would negatively impact performance or readability.
  3. Forgetting to declare functions as inline when appropriate: If you have small, frequently called functions that could benefit from inlining, make sure to declare them as inline.
  4. Overusing inline functions for the sake of performance: Inlining should be used judiciously and only when it provides a measurable improvement in performance. Overuse can lead to increased code size, decreased readability, and longer compile times without any significant benefits.
  5. Ignoring the potential downsides of inlining: Inlining can make debugging more difficult, increase code size, and extend compile times. Be aware of these potential downsides and weigh them against the benefits when deciding whether to inline a function.

Practice Questions

  1. Write an inline function that swaps two integers without using a temporary variable.
  2. Given the following non-inline function:
int sum(int a, int b) {
return a + b;
}

Write an equivalent inline function and explain why it might be more efficient than the original function in certain scenarios.

  1. Write an inline function that calculates the maximum of two integers without using any comparison operators (<, <=, >, >=).
  2. Implement an inline function that finds the greatest common divisor (GCD) of two integers using Euclid's algorithm and memoization.
  3. Modify the factorial example to calculate the sum of all numbers from 1 to N using both a non-inline function and an inline function with memoization. Compare their performance.

FAQ

  1. Why doesn't the compiler always inline a function marked as inline?

The compiler has the final say on whether to inline a function, and it may choose not to do so if inlining would negatively impact performance or readability.

  1. Can I force the compiler to inline a function?

There is no way to force the compiler to inline a function. However, you can write small, frequently called functions and declare them as inline to encourage the compiler to inline them when appropriate.

  1. What are some situations where inlining a function might be beneficial?

Inlining can be beneficial for small, frequently called functions that have simple bodies and little or no side effects. These functions can potentially benefit from reduced function call overhead and improved performance.

  1. Are there any downsides to using inline functions?

Yes, inlining can lead to increased code size, decreased readability, and longer compile times. It can also make debugging more difficult due to the repeated expansion of the function body at multiple points in your program.

  1. How do I measure the performance impact of using inline functions?

You can use a profiler or timing functions (such as those provided by the C++ Standard Library) to measure the execution time of different parts of your code and compare the performance of inline and non-inline versions of functions. Additionally, you can use tools like gcc -O2 or clang -O3 to enable optimizations and observe the impact on inlined functions.

inline function (C++) | C++ | XQA Learn