www.delorie.com/gnu/docs/m4/m4_20.html   search  
Buy GNU books!

GNU macro processor

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.2 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''))
=>expansion text

See section 3.4 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'')
=>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'.')
=>This is macro foo.

The `foo' in the expansion text is not expanded, since it is a quoted string, and not a name.

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003