I need to understand how this code works:
#define foo1( a ) (a * a) // How does this work?
inline int foo2(
The difference is, the foo1 defined by #define is NOT a function, while foo2 is.
In compilation process, the compiler will replace the foo1(parameter) keyword in your code with (parameter * parameter).
Meaning,
cout << "foo1 = " << foo1( 1+2 ) << "\n"; // How does this work
?
will be replaced by the following code,
cout << "foo1 = " << ( 1+2 * 1+2 ) << "\n"; // How does this work
?
(Because the parameter here is 1+2, instead of 3.)
The result is 1+2 * 1+2, which is 5.
Now let look at foo2, since it is an inline function, the compiler will not replace it. When your code is compiled into an executable file, and the executable file is executed, the 2 + 1 expression will be calculated first, and the result, 3, will then be passed to foo2().
In conclusion, the difference really lies in the compilation of your code. You may need more knowledge on what happens there.