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

C++ के Inline Functions

एक inline function एक hint है कि compiler को function के steps इस्तेमाल की जगह ही paste कर देने चाहिए, किसी दूसरी जगह flip करने की बजाय एक recipe अपने page पर copy करने जैसा। यह tiny functions पर थोड़ा समय बचा सकता है।
Syntax
cpp
inline return_type function_name(parameters) {
  return expression;
}

inline Keyword

एक function definition से पहले inline keyword add करना compiler को suggest करता है कि यह एक normal function call generate करने की बजाय हर call site पर function का actual code paste कर दे।

यह बहुत छोटे, frequently-called functions के लिए एक अलग function पर jump करने और वापस return करने की छोटी performance cost खत्म कर सकता है।

उदाहरण: 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;
}

Inline Functions क्यों इस्तेमाल करें

एक regular function call की real runtime overhead होती है — CPU को अपनी current state save करनी होती है, function के code पर jump करना होता है, और बाद में वापस jump करना होता है — और सिर्फ एक-दो lines की logic वाले एक function के लिए, वह overhead function actually करने वाले काम से बड़ी हो सकती है।

Inlining ऐसे छोटे functions के लिए वह overhead पूरी तरह हटा देती है।

उदाहरण: 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

छोटी conversion या math utilities, जैसे inline double toFahrenheit(double celsius) { return celsius * 9/5 + 32; }, inline के लिए ideal candidates हैं क्योंकि वे छोटी हैं, frequently call होती हैं, और call overhead skip करने से सबसे ज़्यादा relative benefit पाती हैं।

उदाहरण: 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

inline keyword compiler के लिए सिर्फ एक request है, एक guarantee नहीं — modern compilers इसे ignore करने के लिए free हैं (और अक्सर करते हैं, अपना judgment इस्तेमाल करते हुए) अगर एक function sensibly inline करने के लिए बहुत बड़ा या complex हो, और इसके उलट उन functions को inline कर सकते हैं जिन्हें आपने कभी inline mark नहीं किया अगर वे decide करें कि यह worthwhile है।

उदाहरण: 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;
}

Inline Functions के Drawbacks

कई अलग-अलग जगहों से call होने वाले functions पर inline को overuse करना compiler को हर call site पर उस function का code duplicate करने का कारण बनता है, जो final compiled binary का size noticeably bloat कर सकता है।

इसे आदत से broadly apply करने की बजाय genuinely छोटे, hot-path functions के लिए reserve करें।

उदाहरण: 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. #}
आम गलतियां
  1. यह मान लेना कि inline compiler को function inline करने पर force करता है, जब यह सिर्फ एक hint है।
  2. बड़े functions को inline करना, जो executable bloat करता है।
  3. एक inline function को सिर्फ एक .cpp file में define करना और इसे किसी दूसरी से call करना, जो link होने में fail होता है; इसे हर उस file में visible होना चाहिए जो इसे इस्तेमाल करती है।

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.