Macro Functions

Macro Functions

It is often helpful to group a set of statements together into a single function, which can then be called as needed. NBC supports macro functions with arguments. Values may be returned from a macro function by changing the value of one or more of the arguments within the body of the macro function.

Macro functions are defined using the following syntax:

 #define name(argument_list) \
   // body of the macro function \
   // last line in macro function body has no '\' at the end

Please note that the newline escape character ('\') must be the very last character on the line. If it is followed by any whitespace or comments then the macro body is terminated at that point and the next line is not considered to be part of the macro definition.

The argument list may be empty, or it may contain one or more argument definitions. An argument to a macro function has no type. Each argument is simply defined by its name. Multiple arguments are separated by commas. Arguments to a macro function can either be inputs (constants or variables) for the code in the body of the function to process or they can be outputs (variables only) for the code to modify and return. The following sample shows how to define a macro function to simplify the process of drawing text on the NXT LCD screen:

 #define MyMacro(x, y, berase, msg) \
   mov dtArgs.Location.X, x \
   mov dtArgs.Location.Y, y \
   mov dtArgs.Options, berase \
   mov dtArgs.Text, msg \
   syscall DrawText, dtArgs

 MyMacro(0, 0, TRUE, 'testing')
 MyMacro(10, 20, FALSE, 'Please Work')

NBC macro functions are always expanded inline by the NBC preprocessor. This means that each call to a macro function results in another copy of the function's code being included in the program. Unless used judiciously, inline macro functions can lead to excessive code size.

Generated by  doxygen 1.6.2