I came across a c++ code where a function was defined in the header section of the file as follows
#define APPEND_VALUE(X, Y, I)\ {\ int idx = (Y*100+X);\ int idxn = idx + ValueCount[idx];\ TempVector[idxn] = I;\ CountVector[idx] += 1;\ } (Note that this is not all the code and TempVector and CountVector was defined elsewhere)
Later in the code APPEND_VALUE was used like any other function. I was wondering what is the difference between the above (#define APPEND_VALUE) code and the below code
void APPEND_VALUE(int X, int Y, int I) { int idx = (Y*100+X); int idxn = idx + ValueCount[idx]; TempVector[idxn] = I; CountVector[idx] += 1; } What is the advantage of using one over the other? also is there a technical name for defining a function as show in the first code(the one using #define).
APPEND_VALUE(5, 2+3, 5);.APPEND_VALUE(...)(with proper arguments of course) then it basically replaces that with the body of the macro. Most compilers have options to let you see the code after preprocessing, I suggest you do that to see how the macro have been expanded.