---
title: "Inline function: significance and limits"
subject: "Object Oriented Programming"
chapter: 3
key: "semester_3/object_oriented_programming:inline-function"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/inline-function
site: BCT Engineering
---

# Inline function: significance and limits

Object Oriented Programming · Chapter 3: C++ Language Constructs

- Asked in: 2073 Shrawan, 2 marks; 2072 Kartik, 3 marks; 2069 Chaitra, 4 marks (asked 3 times)
- Video: [C++ Inline Function | Learn Coding](https://www.youtube.com/watch?v=mlGNIILfVvI) — Learn Coding
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/inline-function
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-3

## Question

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?

## 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.

## Answer

#### 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.

```cpp
#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**

```text
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 `#define` macros.
- 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`) or `switch` or `goto`,
- contains `static` variables,
- is recursive,
- is large or complex,
- has its address taken (a pointer to the function is used).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What does inline remove?
   A. Function call overhead
   B. Type checking
   C. Parameters
   D. Return values
   Answer: A. Body substituted. (Key point: Significance)

2. Main drawback?
   A. Slower
   B. Code size grows
   C. No type check
   D. Cannot return
   Answer: B. Body copied at every call. (Key point: Disadvantage)

3. Which makes the compiler likely ignore inline?
   A. Simple return
   B. One param
   C. Loops, recursion, static variables
   D. int return
   Answer: C. Listed. (Key point: When not working)

4. Advantage over macros?
   A. Faster always
   B. No keyword
   C. Bigger
   D. Type checked, args evaluated once
   Answer: D. As stated. (Key point: Vs macro)

## Related in other subjects

- [Wave function significance; time independent equation](https://www.bctengineer.com/question.md?key=semester_1%2Fengineering_physics%3Aschrodinger-time-independent-significance) — Engineering Physics, chapter 10, asked 2×
- [Significance of main() function](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asignificance-of-main) — Computer Programming, chapter 6
- [Unit impulse function and its significance](https://www.bctengineer.com/question.md?key=semester_5%2Fdata_communication%3Aunit-impulse-significance) — Data Communication, chapter 2
- [Photoelectric effect: work function of sodium](https://www.bctengineer.com/question.md?key=semester_3%2Felectrical_engineering_material%3Aphotoelectric-sodium-work-function) — Electrical Engineering Material, chapter 1, asked 2×
