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

C++ Inline Functions

An inline function is a hint that the compiler should paste the function's steps right where it is used, like copying a recipe onto your page instead of flipping to another one. It can save a little time on tiny functions.
Syntax
cpp
inline return_type function_name(parameters) {
  return expression;
}

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 std::cout and std::cin
#include <iostream>

inline int square(int x) {
	// Send x * x back to the caller
	return x * x;
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << square(5) << std::endl;
	// Return 0 to signal that the program finished successfully
	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 std::cout and std::cin
#include <iostream>

inline double toFahrenheit(double celsius) {
	// Send celsius * 9 / 5 + 32 back to the caller
	return celsius * 9 / 5 + 32;
}

// Program execution starts in main()
int main() {
	// Print to the console with cout
	std::cout << toFahrenheit(20.0) << std::endl;
	// Return 0 to signal that the program finished successfully
	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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming inline forces the compiler to inline the function, when it is only a hint.
  2. Inlining large functions, which bloats the executable.
  3. Defining an inline function only in a .cpp file and calling it from another, which fails to link; it must be visible in each file that uses it.

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.