Programming tools / The preprocessor |
Function macros are preprocessor macros which can take arguments.
&define identifier( arglist ) body
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, parenthesis are required for the macro to be expanded. As we can see in the next example, the third line is not expanded because it there is no '()' 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 parenthesis. In this example, the second line has been substituted, but th 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)
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(,,)
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"