We can also write macros that accept arguments. Here's an example of a macro with an argument:
#define println(X) cout << X << endl;
What this macro will do is every time println("Some value")
is encountered in the code, the code on the right-hand side (cout << "Some value" << endl
) will be copied and pasted on the console. Notice how the argument between the brackets is copied in the place of X
. Say we had the following line of code:
println( "Hello there" )
This will be replaced by the following statement:
cout << "Hello there" << endl;
Macros with arguments are exactly like very short functions. Macros cannot contain any newline characters in them.