← Back to C++ Course | Chapter 5: Functions | Lesson 10 of 13

C++ Inline Functions

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

cpp
#include <iostream>

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

int main() {
	std::cout << square(5) << std::endl;
	return 0;
}

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

cpp
#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;
}

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

cpp
#include <iostream>

inline double toFahrenheit(double celsius) {
	return celsius * 9 / 5 + 32;
}

int main() {
	std::cout << toFahrenheit(20.0) << std::endl;
	return 0;
}

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

cpp
#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;
}

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

cpp
#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 run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.