Sobes.tech
Junior — Middle

What is the purpose of the inline keyword in programming?

sobes.tech AI

Answer from AI

The keyword inline in C/C++ is used to suggest to the compiler to replace a function call with the actual body of the function at the call site, to avoid the overhead of a function call. This can improve performance, especially for small functions that are called frequently.

However, the compiler is not obliged to inline the function; it is merely a recommendation. Besides optimization, inline also allows defining a function in a header file without violating the rules of multiple definitions.

Example:

inline int square(int x) {
    return x * x;
}

int main() {
    int a = square(5); // the compiler may replace the call with 5 * 5
}