Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🔵 Core C++  ·  Lesson 32

Inline Functions in C++

In this lesson: What inline functions are in C++, when the compiler inlines, advantages and limits — with example. In Hindi & English.

What is an inline function?

A normal function call has a small cost: the program jumps to the function, passes arguments, and jumps back. For a one-line function such as "return the larger of two numbers", that overhead can be bigger than the work itself. An inline function asks the compiler to paste the function's body directly at each place it is called, removing the jump.

inline is a request, not a command. The compiler decides whether inlining is worth it.

This lesson covers the beginner view — syntax, examples, advantages and limits. For the deeper rules about headers and the One Definition Rule, see C++ inline functions: ODR, headers and performance.

Syntax

C++
inline return_type function_name(parameters) {
    // small body
}

The keyword goes before the return type in the function definition.

Example: square and 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

If the compiler inlines square(7), the generated code is as if you had written 7 * 7 directly — no call at all.

Inline member functions

A member function defined inside the class body is automatically inline:

C++
class Circle {
    double r;
public:
    Circle(double radius) : r(radius) {}
    double area() const { return 3.14159 * r * r; }   // implicitly inline
    double perimeter() const;                          // declared only
};

inline double Circle::perimeter() const {              // defined outside: add inline
    return 2 * 3.14159 * r;
}

Getters and setters are the classic example of small functions that benefit from this.

Inline function vs macro

In C, programmers used macros for speed. The same idea as a macro has hidden traps:

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

int i = 3;
int a = SQUARE_M(i++);   // i incremented twice — undefined/buggy
int j = 3;
int b = squareF(j++);    // j incremented once, b = 9
PointMacroInline function
Processed byPreprocessor (text)Compiler (real function)
Type checkingNoneYes
Arguments evaluatedPossibly many timesExactly once
Scope and namespacesIgnoredRespected
DebuggingHardNormal

When will the compiler not inline?

The compiler usually refuses (or it gives no benefit) when the function:

  • is large or contains loops, switch or goto,
  • is recursive,
  • contains static variables,
  • is called through a function pointer or its address is taken,
  • is virtual and called through a base pointer (resolved at run time).

Modern compilers with optimisation (-O2) also inline small functions you did not mark inline. So use inline for tiny functions defined in headers, not as a speed trick for big ones.

Advantages and disadvantages

AdvantagesDisadvantages
No call/return overhead for tiny functionsCode size grows — the body is copied at every call
Enables further compiler optimisation at the call siteLarger code can hurt CPU cache and even slow things down
Safer than macros (type-checked)Changing an inline function in a header forces recompilation of every file that includes it
Can be defined in a header used by many filesNot suitable for large or recursive functions

Common mistakes

  • Marking large or recursive functions inline and expecting speed.
  • Declaring a function inline in a header but defining it in a .cpp file — other files then get "undefined reference" errors. Inline functions must be defined where they are used, usually in the header.
  • Thinking inline guarantees inlining.
  • Using a macro when an inline (or constexpr) function would be safer.
🏋️ Practice

Write inline functions cube(int), isEven(int) and celsiusToFahrenheit(double). Use them in a loop over an array of values, and write the same cube as a macro to see what happens with cube(i++).

Summary

  • inline asks the compiler to replace calls with the function body.
  • Best for very small, frequently called functions.
  • Member functions defined inside a class are inline automatically.
  • Inline functions are type-safe and evaluate arguments once — unlike macros.
  • The compiler may ignore inline; don't use it on big or recursive functions.

Related: functions, preprocessor directives, inline functions in depth.

Frequently Asked Questions

What is an inline function in C++?
An inline function is a function marked with the inline keyword (or defined inside a class body). It asks the compiler to replace each call with the function's code instead of performing a normal function call, which can save call overhead for very small functions.
Does the compiler always make an inline function inline?
No. inline is only a request. The compiler may ignore it for functions that are large, recursive, contain loops or static variables, or whose address is taken — and it may inline small functions that are not marked inline at all. Today the keyword mainly allows the function to be defined in a header.
What is the difference between an inline function and a macro?
A macro is text substitution by the preprocessor with no type checking, and its arguments can be evaluated more than once. An inline function is a real function: arguments are type-checked and evaluated exactly once, it respects scope, and it can be debugged.
Are member functions defined inside a class inline?
Yes. A member function whose body is written inside the class definition is implicitly inline, so you do not need to write the inline keyword for it.
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicScope and Storage Classes Your progress: 32 / 81 (40%) · Lesson 33 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free