C++ Inline Functions
Inline functions in C++ are functions for which the compiler attempts to insert the complete body of the function wherever the function is called. This can reduce function call overhead and improve performance for small, frequently called functions.
Inline functions are defined using the inline keyword before the function definition.
Syntax
inline return_type function_name(parameters) {
// function code
}
Example Program
#include <iostream>
using namespace std;
// Inline function to add two numbers
inline int add(int a, int b) {
return a + b;
}
int main() {
int sum = add(10, 20); // Function call
cout << "Sum: " << sum << endl;
return 0;
}
Output
Sum: 30
Important Notes
- Inline functions are generally used for small, frequently called functions to reduce the overhead of function calls.
- The compiler may ignore the inline request for large or complex functions.
- Excessive use of inline functions can increase the size of the binary code.
Next Topic
Next, learn about Recursive Functions in C++.