- Asked 3/10
- 2073 Shrawan · 2 marks
- 2072 Kartik · 3 marks
- 2069 Chaitra · 4 marks
Inline function: significance and limits
Short answer
Inline functions replace call with code to eliminate overhead, e.g., inline int square(int x){return x*x;}. They are type-safe unlike macros but increase binary size. Inlining may be ignored for loops, recursion, static variables, or large functions.
What is the significance of using inline function? Describe with suitable example. What are the advantages and disadvantages of using inline function? When may an inline function not work?
Significance of inline function
An inline function is a function whose body is substituted at the place of each call by the compiler, instead of doing a normal function call. A normal call costs time for pushing arguments, jumping to the function and returning. Inline substitution removes this overhead, so small functions run faster. It is requested with the inline keyword.
#include <iostream>
using namespace std;
inline int square(int x) { // request for inline expansion
return x * x;
}
int main() {
cout << square(5) << endl; // compiler treats it like: 5 * 5
cout << square(8) << endl;
return 0;
}
Output
25
64
Unlike a macro, an inline function is type checked and evaluates its arguments only once.
Advantages
- Saves function call overhead, so execution is faster.
- Type safe, unlike
#definemacros. - Keeps the readability of functions while giving the speed of inline code.
- Member functions defined inside a class are inline by default.
Disadvantages
- Code size grows, because the body is copied at every call.
- Large inline code may reduce cache efficiency and make the executable big.
- Changing an inline function requires recompiling all files that use it.
When inline may not work
inline is only a request; the compiler may ignore it. It usually does not work when the function:
- contains loops (
for,while,do-while) orswitchorgoto, - contains
staticvariables, - is recursive,
- is large or complex,
- has its address taken (a pointer to the function is used).
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Wave function significance; time independent equation · Engineering Physics, chapter 10 · asked 2×
- Significance of main() function · Computer Programming, chapter 6
- Unit impulse function and its significance · Data Communication, chapter 2
- Photoelectric effect: work function of sodium · Electrical Engineering Material, chapter 1 · asked 2×