Go to the first, previous, next, last section, table of contents.


Arguments to macros

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. Here is a 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 section Quoting macro arguments, for an explanation of the double quotes.

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 zero'th 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.


Go to the first, previous, next, last section, table of contents.