Contents | < Browse | Browse >
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

      For an explanation of the double quote see Quoting Arguments.

   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.