C++ Inline Functions
In this page:
The inline Keyword
Adding the inline keyword before a function definition suggests to the compiler that it should paste the function's actual code directly at each call site, rather than generating a normal function call. This can eliminate the small performance cost of jumping to and returning from a separate function for very short, frequently-called functions.
Example: The inline Keyword
#include <iostream>
inline int square(int x) {
return x * x;
}
int main() {
std::cout << square(5) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Why Use Inline Functions
A regular function call has real runtime overhead — the CPU has to save its current state, jump to the function's code, and jump back afterward — and for a function with only one or two lines of logic, that overhead can be larger than the work the function actually does. Inlining removes that overhead entirely for such small functions.
Example: Why Use Inline Functions
#include <iostream>
inline int add(int a, int b) {
return a + b; // no call/jump/return overhead when inlined
}
int main() {
std::cout << add(2, 3) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Math Utility Conversions
Small conversion or math utilities, like inline double toFahrenheit(double celsius) { return celsius * 9/5 + 32; }, are ideal candidates for inline since they're short, called frequently, and gain the most relative benefit from skipping call overhead.
Example: Math Utility Conversions
#include <iostream>
inline double toFahrenheit(double celsius) {
return celsius * 9 / 5 + 32;
}
int main() {
std::cout << toFahrenheit(20.0) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Compiler Discretion
The inline keyword is only a request to the compiler, not a guarantee — modern compilers are free to ignore it (and often do, using their own judgment) if a function is too large or complex to sensibly inline, and conversely may inline functions you never marked inline at all if they decide it's worthwhile.
Example: Compiler Discretion
#include <iostream>
inline int triple(int x) {
return x * 3; // "inline" is only a request -- the compiler may ignore it
}
int main() {
std::cout << triple(4) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Drawbacks of Inline Functions
Overusing inline on functions that are called from many different places causes the compiler to duplicate that function's code at every call site, which can noticeably bloat the size of the final compiled binary. Reserve it for genuinely small, hot-path functions rather than applying it broadly out of habit.
Example: Drawbacks of Inline Functions
#include <iostream>
inline int longFunction(int x) {
int result = x;
for (int i = 0; i < 5; i++) {
result += i * x;
}
return result; // large inline functions duplicated at every call site bloat binary size
}
int main() {
std::cout << longFunction(3) << std::endl;
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions