what's the difference between macro and functions?
What's the advantage and disadvantage of each one?
Show me an example showing the diference if possible.
Macros are textual substitutions performed during the preprocessing phase.
The advantage of macros over non-inlined functions is that you avoid the overhead of a function call
and return. The disadvantage of macros are the subtle side effects that can happen. L B gave just
one example. The canonical example is this one:
1 2 3 4 5 6 7 8
#define MIN( a, b ) a < b ? a : b
int main() {
int a = 5;
int b = 6;
std::cout << MIN( a++, b++ ) << std::endl;
}
What is the value of a and b at the end of the program? Hint: it is NOT 6 and 7.
For this reason alone inline functions are *always* preferred to macros.
EDIT: And for that matter, what value is output by the cout statement above?