Inline expansion, or inlining, is compiler optimization that replaces a function call site with the body of the callee. This optimization may improve time and space usage at runtime, at the possible cost of increasing the final size of the program (i.e. the binary file size).
#include <iostream>
using namespace std;
inline int square(int n)
{
return n*n;
}
int main()
{
int x = 5, sq;
sq = square(x);
//
// At this call, x*x is replaced by compiler.
//
cout << sq << endl;
return 0;
}
"inline" is just a hint or request to compiler to expand the body of function at its call.
Compiler may decline this request, often in following cases:
1. Function returning a value, contains loop, switch or goto.
2. Function not returning value, has a return statement.
3. Function contains a static variable.
4. Function is recursive.