Introduction:
You can use macros within your GCBASIC code.
Macros are similar to subroutines, but during compilation, everything is inserted inline. This may increase the code size slightly, but it also reduces stack usage.
Parameters are handled in a similar way to how constants are handled, so there is a lot more freedom when passing things into a macro (unlike subs or functions, where everything must be stored in a variable).
For example, for PulseOut one parameter is a pin, and the other is a time length like "500 ms". Neither of those parameters could be stored in a variable,
but passing them in as macro parameters is possible.
Demonstration Program:
'PulseOut Macro
macro Pulseout (Pin, Time)
Set Pin On
Wait Time ' <<< a parameter used directly as a time literal, impossible with a sub or function
Set Pin Off
end macroKey line: Wait Time — because macro parameters are substituted like constants rather than copied into variables, calling Pulseout(PORTB.0, 500 ms) compiles this line directly to Wait 500 ms; a Sub or Function parameter cannot accept a time literal like 500 ms this way.
See Also:
- Measuring a Pulse Width — a macro used for timing-critical inline code
- Implementing a method with a Pin name as a parameter — a macro used to pass a pin constant

