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

Last Updated : 26 Apr, 2026

An inline function is a function in C++ whose code is expanded at the point of call at compile time. It reduces function-call overhead.

#include using namespace std;

inline int getSum(int a, int b){ return a + b; }

int main(){ int num1 = 5, num2 = 10;

int result = getSum(num1, num2);
cout << "Sum: " << result << endl;
return 0;

}

`

Need for Inline Functions

compilation

Inline expands code at compile time.

**Code Example:

C++ `

#include using namespace std;

inline void displayMessage(){ for (int i = 0; i < 5; i++){ cout << "Hello " << i << endl; } } int main(){ displayMessage(); return 0; }

`

Output

Hello 0 Hello 1 Hello 2 Hello 3 Hello 4

**Explanation:

Behaviour of Inline Functions

Virtual Functions Inlining

**Inline vs Macros

In C++, both inline functions and macros reduce function call overhead for faster execution, but they differ in behavior. Inline functions provide better safety and scoping, while macros are simple preprocessor directives. The table below shows their main differences.

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