- Functions with small code can be declared as inline.
- Wherever an inline function is called, actual function code will be replaced
- Syntax: Put keyword inline before function definition and not in declaration. For e.g.
void swap(int& a,int& b); //declaration
inline void swap(int& a,int& b) //defintion
temp = a;
a = b;
Inline Function Vs Macros
- Unlike inline functions, macros do not have type-checking and also do not check if arguments are well-formed.
- You cannot return a value as computation result in a macro.
- Macro uses textual substitution, so there may be side-effects and inefficiencies due to re-evaluation of arguments and order of operators.
- Macro expanded code is difficult to understand for compilation errors.
- Debugging information for inline functions is more helpful
- Sometimes, required construct cannot be written using macros or may be it will awkward to do so.
Points to Ponder
- Member functions defined inside the class definition are inline by default.
- Compiler decides which function to use as inline. So, even if you have declared a function inline, compiler can treat it as normal function. In this case, compiler will use calling instead of replacing the function code at the calling places.
- Inline functions may increase the performance significantly.
- May because inline functions increases code size to be compiled and therefore compilation time.