Inline Functions in C++ (original) (raw)

Last Updated : 14 May, 2025

In C++, **inline functions provide a way to optimize the performance of the program by reducing the overhead related to a function call. When a function is specified as **inline the whole code of the inline function is inserted or substituted at the point of its call during the compilation instead of using the normal function call mechanism.

**Example:

C++ `

#include using namespace std;

// Inline function inline int square(int x) { return x * x; }

int main() { int num = 5;

// Calling inline function
int res = square(num);
cout << res;
return 0;

}

`

**Syntax

The inline keyword is used to write inline functions.

C++ `

inline return_type function_name(params)...

`

The inline keyword suggests the compiler that it should replace the functions call with the actual code of the function to avoid the overheads of the function call. Inlining a functions is only a request to the compiler, not a command. **The compiler may not perform inlining in such circumstances as:

  1. If a function contains a loop.
  2. If a function contains static variables.
  3. If a function is recursive.
  4. If a function return type is other than void, and the return statement doesn’t exist in a function body.
  5. If a function contains a switch or goto statement.

To learn more about performance optimization and inline functions, check out our Complete C++ Course, which covers inline functions, optimization strategies, and high-performance programming techniques.

Need for Inline Functions

When a function is called, the CPU stores the return address, copies arguments to the call stack, and transfers control to the function. After execution, the return value is stored, and control is returned to the caller. This overhead can be significant for small, frequently used functions, as their execution time is less than the time spent on the call and return process.

This is where the inline functions shine. They remove this overhead by substituting the code of the function in place of a function call.

One other thing to remember is that it is only useful to make the function inline if the time spent during a function call is more compared to the function body execution time.

**An example where the inline function has no effect at all:

C++ `

inline void show() { cout << "value of S = " << S << endl; }

`

The above function takes a relatively long time to execute. In general, a function that performs an input-output (I/O) operation shouldn’t be defined as inline because it spends a considerable amount of time.

Appropriate use of inline functions can provide performance enhancement, but if inline functions are used arbitrarily, then they can’t provide better results. In other words, don’t make every function inline. It is better to keep inline functions as small as possible.

**Inline Functions in Class

All the functions defined inside the class are implicitly inline. Thus, all the restrictions of inline functions are also applied here. If you need to explicitly declare an inline function in the class, then just declare the function inside the class and define it outside the class using the inline keyword.

**Example:

C++ `

#include using namespace std;

class A { public:

// declare inline
inline int square(int x);

};

// Define the function inline int A::square(int x) { return x*x; }

int main() { A obj; cout << obj.square(3); return 0; }

`

Virtual Functions Inlining

C++ compiler cannot inline the virtual function. The reason is that the calls to a virtual function are resolved at runtime instead of compile time. Virtual means waiting until runtime, and inline means during compilation. If the compiler doesn’t know which function will be called, how can it perform inlining?

**Inline vs Macros

In C++ language, both inline functions and macros are used for faster execution of a program by removing the overheads of function calls, but they differ significantly in their implementation and behavior. While both are designed to improve performance, inline functions offers more safety and scoping benefits while macros are just simple preprocessor directives.

The table below lists the primary differences between inline functions and macros:

**Aspect **Inline Functions **Macros
**Definition Inline functions are functions defined with the inline keyword. Macros are preprocessor directives defined using. #define.
**Scope Inline functions have scope and type checking, like regular functions. Macros have no scope or type checking. They are replaced by the preprocessor.
**Evaluation of Arguments Arguments are evaluated once. Arguments may be evaluated multiple times (e.g., in expressions).
**Handling Inline functions are handled by the compiler. Macros are handled by the preprocessor.
**Private Members Can access private members of a class. Cannot access private members of a class.
**Execution Overhead Compiler may ignore the inline request if the function is too large. Macros are always substituted into code.
**Recursion Inline functions can call themselves recursively. Macros cannot be recursive.

**Advantages

**Disadvantages