C++ के Inline Functions
In this page:
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
Compiler Discretion
inline keyword compiler के लिए सिर्फ एक request है, एक guarantee नहीं — modern compilers इसे ignore करने के लिए free हैं (और अक्सर करते हैं, अपना judgment इस्तेमाल करते हुए) अगर एक function sensibly inline करने के लिए बहुत बड़ा या complex हो, और इसके उलट उन functions को inline कर सकते हैं जिन्हें आपने कभी inline mark नहीं किया अगर वे decide करें कि यह worthwhile है।
उदाहरण: Compiler Discretion
#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;
}
Login to try C/C++/Java/PHP code in the editor
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
#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;
}
Login to try C/C++/Java/PHP code in the editor
- यह मान लेना कि
inlinecompiler को function inline करने पर force करता है, जब यह सिर्फ एक hint है। - बड़े functions को inline करना, जो executable bloat करता है।
- एक inline function को सिर्फ एक
.cppfile में define करना और इसे किसी दूसरी से call करना, जो link होने में fail होता है; इसे हर उस file में visible होना चाहिए जो इसे इस्तेमाल करती है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first:
- C++ Functions Introduction
- C++ Function Parameters
- C++ Multiple Function Parameters
- C++ Passing by Reference
- C++ Passing Structures to Functions
- C++ Return Values
- C++ Function Overloading
- C++ Default Arguments
- C++ Recursion
- C++ Inline Functions
- C++ Lambda Functions
- C++ Scope & Lifetime
- C++ Math Functions