Function macro definition
Function macros are preprocessor macros which can take arguments.
Syntax
&define identifier( arglist ) body
- identifier is the name of the macro. Any valid identifier can be used.
- body is any sequence of tokens until the end of the line.
- arglist is a list of identifiers separated with commas and optionally white space.
- There must be no space or comment between the macro name and the opening
parenthesis
(
. Otherwise the macro is not a function macro, but a simple macro.
Usage
Function macros take arguments that are replaced in the body by the preprocessor.
&define function_macro(a,b) a + b
&define simple_macro (a,b) a + b
function_macro( 4 , 5 )
simple_macro (1,2)
& 1 "A"
4 + 5
(a,b) a + b (1,2)
A function macro can have an empty argument list. In this case, parentheses ()
are required for the macro to be expanded. As we can see in the following example, the third line is
not expanded because there is no parentheses after foo
. The function macro cannot
be applied even if it has no arguments.
&define foo() yes
foo()
foo
& 1 "A"
yes
foo
The comma separates arguments. Macro parameters containing a comma can be used with parentheses. In this example, the second line has been substituted, but the third line produced an error, because the number of parameters is incorrect.
&define one_parameter(a) a
one_parameter((a,b))
one_parameter(a,b)
fglcomp -M
output
source.4gl:3:1:3:1:error:(-8039) Invalid number of parameters
for macro one_parameter.
Macro arguments are completely expanded and substituted before the function macro expansion.
A macro argument can be left empty.
&define two_args(a,b) a b
two_args(,b)
two_args(,)
two_args()
two_args(,,)
fglcomp -M
output
source.4gl:4:1:4:1:error:(-8039) Invalid number of parameters
for macro two_args.
source.4gl:5:1:5:1:error:(-8039) Invalid number of parameters
for macro two_args.
Macro arguments appearing inside strings are not expanded.
&define foo(x) "x"
foo(toto)
& 1 "A"
"x"