Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
🔵 Core C++  ·  Lesson 32

Inline Functions

Inline function क्या है?

सामान्य function call की एक छोटी कीमत होती है: program function पर jump करता है, arguments भेजता है और वापस आता है। "दो numbers में बड़ा लौटाओ" जैसे एक line के function में यह overhead असली काम से भी ज़्यादा हो सकता है। Inline function compiler से कहता है कि function का body सीधे हर call वाली जगह पर रख दे, ताकि jump ही न हो।

inline एक अनुरोध है, आदेश नहीं। Inline करना फ़ायदेमंद है या नहीं, यह compiler तय करता है।

यह lesson beginners के लिए है — syntax, उदाहरण, फ़ायदे और सीमाएँ। Headers और One Definition Rule के गहरे नियमों के लिए देखें C++ inline functions: ODR, headers और performance।

Syntax

C++
inline return_type function_name(parameters) {
    // छोटा body
}

Keyword function की definition में return type से पहले लिखा जाता है।

उदाहरण: square और maximum

C++
#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;
}
Square of 7 : 49 Max(45, 62) : 62 Highest mark: 91

अगर compiler square(7) को inline करे, तो बना code ऐसा होगा जैसे आपने सीधे 7 * 7 लिखा हो — कोई call नहीं।

Inline member functions

Class body के अंदर define किया गया member function अपने-आप inline होता है:

C++
class Circle {
    double r;
public:
    Circle(double radius) : r(radius) {}
    double area() const { return 3.14159 * r * r; }   // अपने-आप inline
    double perimeter() const;                          // सिर्फ declared
};

inline double Circle::perimeter() const {              // बाहर defined: inline जोड़ें
    return 2 * 3.14159 * r;
}

Getters और setters ऐसे छोटे functions का classic उदाहरण हैं जिन्हें इसका फ़ायदा मिलता है।

Inline function vs macro

C में programmers speed के लिए macros लेते थे। Macro वाले तरीके में छिपे जाल हैं:

C++
#define SQUARE_M(x) ((x) * (x))
inline int squareF(int x) { return x * x; }

int i = 3;
int a = SQUARE_M(i++);   // i दो बार बढ़ता है — गलत/undefined
int j = 3;
int b = squareF(j++);    // j एक बार बढ़ता है, b = 9
बिंदुMacroInline function
कौन process करता हैPreprocessor (text)Compiler (असली function)
Type checkingनहींहाँ
Arguments evaluateशायद कई बारठीक एक बार
Scope और namespacesअनदेखामाना जाता है
Debuggingमुश्किलसामान्य

Compiler कब inline नहीं करेगा?

Compiler आमतौर पर मना करता है (या फ़ायदा नहीं होता) जब function:

  • बड़ा हो या उसमें loops, switch या goto हों,
  • recursive हो,
  • उसमें static variables हों,
  • function pointer से call हो या उसका address लिया गया हो,
  • virtual हो और base pointer से call हो (run time पर तय)।

Optimisation (-O2) वाले modern compilers बिना inline लिखे छोटे functions को भी inline कर देते हैं। इसलिए inline headers में define होने वाले छोटे functions के लिए लें, बड़े functions की speed trick की तरह नहीं।

फ़ायदे और नुकसान

फ़ायदेनुकसान
छोटे functions में call/return overhead नहींCode size बढ़ता है — body हर call पर copy होता है
Call वाली जगह पर compiler और optimisation कर पाता हैबड़ा code CPU cache पर असर डालकर धीमा भी कर सकता है
Macros से सुरक्षित (type-checked)Header में inline function बदलने पर उसे include करने वाली हर file फिर compile होती है
कई files में इस्तेमाल होने वाले header में define हो सकता हैबड़े या recursive functions के लिए ठीक नहीं

आम गलतियाँ

  • बड़े या recursive functions को inline लिखकर speed की उम्मीद करना।
  • Header में function को inline declare करना पर definition .cpp में रखना — दूसरी files में "undefined reference" errors। Inline functions वहीं define होने चाहिए जहाँ इस्तेमाल हों, आमतौर पर header में।
  • यह मानना कि inline से inlining पक्की है।
  • जहाँ inline (या constexpr) function सुरक्षित हो, वहाँ macro लेना।
🏋️ अभ्यास

Inline functions cube(int), isEven(int) और celsiusToFahrenheit(double) लिखें। इन्हें values के array पर loop में इस्तेमाल करें, और यही cube macro के रूप में लिखकर देखें कि cube(i++) के साथ क्या होता है।

सारांश

  • inline compiler से calls की जगह function body रखने को कहता है।
  • बहुत छोटे, बार-बार call होने वाले functions के लिए सबसे अच्छा।
  • Class के अंदर define किए गए member functions अपने-आप inline होते हैं।
  • Inline functions type-safe हैं और arguments एक बार evaluate करते हैं — macros के विपरीत।
  • Compiler inline को अनदेखा कर सकता है; बड़े या recursive functions पर न लगाएँ।

संबंधित: functions, preprocessor directives, inline functions विस्तार से।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में inline function क्या है?
Inline function वह function है जिस पर inline keyword लगा हो (या जो class body के अंदर define हो)। यह compiler से कहता है कि हर call की जगह function का code ही रख दे, सामान्य function call करने के बजाय — इससे बहुत छोटे functions में call का overhead बच सकता है।
क्या compiler हमेशा inline function को inline करता है?
नहीं। inline सिर्फ एक अनुरोध है। बड़े, recursive, loops या static variables वाले, या जिनका address लिया गया हो, ऐसे functions के लिए compiler इसे अनदेखा कर सकता है — और बिना inline लिखे छोटे functions को भी inline कर सकता है। आज यह keyword मुख्य रूप से function को header में define करने की अनुमति देता है।
Inline function और macro में क्या अंतर है?
Macro preprocessor का text replacement है, जिसमें type checking नहीं होती और arguments एक से ज़्यादा बार evaluate हो सकते हैं। Inline function असली function है: arguments type-check होते हैं और ठीक एक बार evaluate होते हैं, यह scope मानता है और debug हो सकता है।
क्या class के अंदर define किए गए member functions inline होते हैं?
हाँ। जिस member function का body class definition के अंदर लिखा हो, वह अपने-आप inline होता है, इसलिए उसके लिए inline keyword लिखने की ज़रूरत नहीं।
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.
अगला topicScope and Storage Classes आपकी प्रगति: 32 / 81 (40%) · Lesson 33 / 81 आगे बढ़ें →

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें