Inline Functions in C++
In this lesson: What inline functions are in C++, when the compiler inlines, advantages and limits — with example. In Hindi & English.
What is an inline function?
A normal function call has a small cost: the program jumps to the function, passes arguments, and jumps back. For a one-line function such as "return the larger of two numbers", that overhead can be bigger than the work itself. An inline function asks the compiler to paste the function's body directly at each place it is called, removing the jump.
inline is a request, not a command. The compiler decides whether inlining is worth it.This lesson covers the beginner view — syntax, examples, advantages and limits. For the deeper rules about headers and the One Definition Rule, see C++ inline functions: ODR, headers and performance.
Syntax
inline return_type function_name(parameters) {
// small body
}The keyword goes before the return type in the function definition.
Example: square and maximum
#include <iostream>
inline int square(int x) {
return x * x;
}
inline int maxOf(int a, int b) {
return (a > b) ? a : b;
}
int main() {
std::cout << "Square of 7 : " << square(7) << "\n";
std::cout << "Max(45, 62) : " << maxOf(45, 62) << "\n";
int marks[] = {78, 91, 66, 84};
int best = marks[0];
for (int m : marks) best = maxOf(best, m);
std::cout << "Highest mark: " << best << "\n";
return 0;
}If the compiler inlines square(7), the generated code is as if you had written 7 * 7 directly — no call at all.
Inline member functions
A member function defined inside the class body is automatically inline:
class Circle {
double r;
public:
Circle(double radius) : r(radius) {}
double area() const { return 3.14159 * r * r; } // implicitly inline
double perimeter() const; // declared only
};
inline double Circle::perimeter() const { // defined outside: add inline
return 2 * 3.14159 * r;
}Getters and setters are the classic example of small functions that benefit from this.
Inline function vs macro
In C, programmers used macros for speed. The same idea as a macro has hidden traps:
#define SQUARE_M(x) ((x) * (x))
inline int squareF(int x) { return x * x; }
int i = 3;
int a = SQUARE_M(i++); // i incremented twice — undefined/buggy
int j = 3;
int b = squareF(j++); // j incremented once, b = 9| Point | Macro | Inline function |
|---|---|---|
| Processed by | Preprocessor (text) | Compiler (real function) |
| Type checking | None | Yes |
| Arguments evaluated | Possibly many times | Exactly once |
| Scope and namespaces | Ignored | Respected |
| Debugging | Hard | Normal |
When will the compiler not inline?
The compiler usually refuses (or it gives no benefit) when the function:
- is large or contains loops,
switchorgoto, - is recursive,
- contains
staticvariables, - is called through a function pointer or its address is taken,
- is
virtualand called through a base pointer (resolved at run time).
Modern compilers with optimisation (-O2) also inline small functions you did not mark inline. So use inline for tiny functions defined in headers, not as a speed trick for big ones.
Advantages and disadvantages
| Advantages | Disadvantages |
|---|---|
| No call/return overhead for tiny functions | Code size grows — the body is copied at every call |
| Enables further compiler optimisation at the call site | Larger code can hurt CPU cache and even slow things down |
| Safer than macros (type-checked) | Changing an inline function in a header forces recompilation of every file that includes it |
| Can be defined in a header used by many files | Not suitable for large or recursive functions |
Common mistakes
- Marking large or recursive functions
inlineand expecting speed. - Declaring a function
inlinein a header but defining it in a.cppfile — other files then get "undefined reference" errors. Inline functions must be defined where they are used, usually in the header. - Thinking
inlineguarantees inlining. - Using a macro when an inline (or
constexpr) function would be safer.
Write inline functions cube(int), isEven(int) and celsiusToFahrenheit(double). Use them in a loop over an array of values, and write the same cube as a macro to see what happens with cube(i++).
Summary
inlineasks the compiler to replace calls with the function body.- Best for very small, frequently called functions.
- Member functions defined inside a class are inline automatically.
- Inline functions are type-safe and evaluate arguments once — unlike macros.
- The compiler may ignore
inline; don't use it on big or recursive functions.
Related: functions, preprocessor directives, inline functions in depth.
Frequently Asked Questions
What is an inline function in C++?
inline keyword (or defined inside a class body). It asks the compiler to replace each call with the function's code instead of performing a normal function call, which can save call overhead for very small functions.Does the compiler always make an inline function inline?
inline is only a request. The compiler may ignore it for functions that are large, recursive, contain loops or static variables, or whose address is taken — and it may inline small functions that are not marked inline at all. Today the keyword mainly allows the function to be defined in a header.What is the difference between an inline function and a macro?
Are member functions defined inside a class inline?
inline keyword for it.