Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · C++ · 04 Jul 2026 · Hindi + English

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

Every function call has hidden overhead: the CPU must save where it was, jump to the function, set up its arguments, run it, and jump back. For a big function this cost is nothing. But for a tiny one-line function called 10 lakh times inside a loop, the overhead can exceed the actual work! It is like driving 2 km to a shop to buy one toffee — the travel costs more than the toffee.

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;
}
25

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

The compiler is free to ignore your inline request — and usually does for functions containing loops, recursion, or large bodies (pasting a big body everywhere would bloat the executable). Modern compilers also inline small functions automatically even without the keyword. So the honest interview answer is: "inline is a hint; the compiler makes the final decision."
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 macroinline function
Processed byPreprocessor (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 समझिए

हर function call में छुपा हुआ overhead है: CPU को याद रखना पड़ता है वह कहां था, function पर jump करना, arguments set करना, चलाना, और वापस jump करना. बड़े function के लिए यह cost कुछ नहीं. लेकिन loop के अंदर 10 लाख बार call होने वाले नन्हे one-line function के लिए overhead असली काम से ज़्यादा हो सकता है! जैसे एक toffee खरीदने के लिए 2 km गाड़ी चलाना — सफर toffee से महंगा पड़ गया.

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;
}
25

Program बिल्कुल वैसा ही behave करता है — same output, same logic — बस उस function की call/return machinery के बिना चलता है.

ज़रूरी: inline request है, order नहीं

Compiler आपकी inline request को ignore करने के लिए आज़ाद है — और loops, recursion या बड़ी body वाले functions के लिए आमतौर पर करता भी है (बड़ी body हर जगह paste करने से executable फूल जाता). Modern compilers छोटे functions को बिना keyword के भी अपने आप inline कर देते हैं. तो ईमानदार interview answer है: "inline एक hint है; final decision compiler का होता है."
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 macroinline 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.