Next: Pseudo Arguments, Previous: Define, Up: Definitions
Macros can have arguments. The nth argument is denoted by
$n
in the expansion text, and is replaced by the nth actual
argument, when the macro is expanded. Replacement of arguments happens
before rescanning, regardless of how many nesting levels of quoting
appear in the expansion. Here is an example of a macro with
two arguments. It simply exchanges the order of the two arguments.
define(`exch', `$2, $1') => exch(`arg1', `arg2') =>arg2, arg1
This can be used, for example, if you like the arguments to
define
to be reversed.
define(`exch', `$2, $1') => define(exch(``expansion text'', ``macro'')) => macro =>expansion text
See Quoting Arguments, for an explanation of the double quotes.
(You should try and improve this example so that clients of exch
do not have to double quote. see Answers)
GNU m4
allows the number following the `$' to
consist of one
or more digits, allowing macros to have any number of arguments. This
is not so in UNIX implementations of m4
, which only recognize
one digit.
As a special case, the zeroth argument, $0
, is always the name
of the macro being expanded.
define(`test', ``Macro name: $0'') => test =>Macro name: test
If you want quoted text to appear as part of the expansion text, remember that quotes can be nested in quoted strings. Thus, in
define(`foo', `This is macro `foo'.') => foo =>This is macro foo.
The `foo' in the expansion text is not expanded, since it is a quoted string, and not a name.