Inline Function in C++: How It Works and When to Use
inline requests the compiler to paste the function body at the call site, removing call overhead for tiny functions. Example, inline vs macro table, and limits.
First understand the cost of a function call
inline: skip the trip, keep the shop at home
inline requests the compiler: "instead of jumping to this function, paste its body directly at every place it is called."
#include <iostream>
using namespace std;
inline int square(int n) { // request: expand me at call site
return n * n;
}
int main() {
cout << square(5) << endl;
// compiler effectively converts the line above to:
// cout << (5 * 5) << endl; <- no jump, no call overhead
return 0;
}
The program behaves identically — same output, same logic — it just runs without the call/return machinery for that function.
Important: inline is a request, not an order
inline int factorial(int n) { // recursive -
return n <= 1 ? 1 : n * factorial(n - 1);
} // compiler will almost certainly NOT inline this - recursion
// cannot be fully pasted (it would never end)
inline vs #define macro — why inline won
#define SQUARE_M(x) ((x) * (x)) // macro: text paste
inline int square_f(int x) { return x * x; } // inline: real function
int a = 3;
cout << SQUARE_M(++a); // expands to ((++a) * (++a)) - a incremented TWICE!
// undefined/unexpected result
int b = 3;
cout << square_f(++b); // ++b evaluated ONCE, passes 4, prints 16. Correct.
| Point | #define macro | inline function |
|---|---|---|
| Processed by | Preprocessor (blind text) | Compiler (real function) |
| Type checking | ❌ None | ✅ Full |
| Argument evaluated once | ❌ Can repeat (++x bug) | ✅ Exactly once |
| Debugger friendly | ❌ Invisible | ✅ Visible |
When to use inline
- Good candidates: tiny getters/setters (
getMarks()), one-line math helpers, functions called inside hot loops. - Bad candidates: functions with loops, recursion, or more than 3-4 lines — the compiler will likely refuse, and forcing it bloats code.
- Note: functions defined inside a class body are implicitly inline — no keyword needed.
- Interview line: "inline trades a little code size for removing call overhead on tiny, frequently-called functions — and it is only a request."
पहले function call की cost समझिए
inline: सफर छोड़ो, दुकान घर में रखो
inline compiler से request करता है: "इस function पर jump करने की बजाय, इसकी body को हर call वाली जगह directly paste कर दो."
#include <iostream>
using namespace std;
inline int square(int n) { // request: मुझे call site पर expand करो
return n * n;
}
int main() {
cout << square(5) << endl;
// compiler ऊपर वाली line को असल में बना देता है:
// cout << (5 * 5) << endl; <- न jump, न call overhead
return 0;
}
Program बिल्कुल वैसा ही behave करता है — same output, same logic — बस उस function की call/return machinery के बिना चलता है.
ज़रूरी: inline request है, order नहीं
inline int factorial(int n) { // recursive -
return n <= 1 ? 1 : n * factorial(n - 1);
} // compiler इसे लगभग पक्का inline NAHI करेगा - recursion
// पूरी paste हो ही नहीं सकती (खत्म ही नहीं होगी)
inline vs #define macro — inline क्यों जीता
#define SQUARE_M(x) ((x) * (x)) // macro: text paste
inline int square_f(int x) { return x * x; } // inline: असली function
int a = 3;
cout << SQUARE_M(++a); // बनता है ((++a) * (++a)) - a DO बार बढ़ा!
// undefined/unexpected result
int b = 3;
cout << square_f(++b); // ++b EK बार evaluate, 4 pass हुआ, 16 print. सही.
| Point | #define macro | inline function |
|---|---|---|
| Process करता है | Preprocessor (अंधा text) | Compiler (असली function) |
| Type checking | ❌ नहीं | ✅ पूरी |
| Argument एक बार evaluate | ❌ Repeat हो सकता है (++x bug) | ✅ Exactly एक बार |
| Debugger friendly | ❌ अदृश्य | ✅ दिखता है |
inline कब use करें
- अच्छे candidates: नन्हे getters/setters (
getMarks()), one-line math helpers, hot loops के अंदर call होने वाले functions. - गलत candidates: loops, recursion, या 3-4 lines से बड़े functions — compiler शायद मना कर देगा, और ज़बरदस्ती करने से code फूलता है.
- Note: class body के अंदर define किए गए functions implicitly inline होते हैं — keyword की ज़रूरत नहीं.
- Interview line: "inline थोड़े code size के बदले छोटे, बार-बार call होने वाले functions का call overhead हटाता है — और यह सिर्फ request है."
Frequently Asked Questions
What is an inline function in C++?
An inline function requests the compiler to paste the function body directly at each call site instead of making a jump, removing call overhead for small frequently-used functions.
Does the compiler always inline an inline function?
No. inline is only a request; compilers typically refuse for recursive, loop-containing or large functions, and modern compilers also inline small functions automatically without the keyword.
Why is an inline function better than a macro?
It is a real function with type checking, single evaluation of arguments and debugger visibility — macros are blind text replacement that can evaluate arguments like ++x multiple times.