Topic: Programming

Online Help


Macro functions and variables


Macro functions and macro variables are convenient ways to organize your code inside a single file and prevent repetitions. They can be inline or multiline. Unlike macro variables, macro functions can have parameters. You can define them by using the following statements:

Inline macro variable:
#def variable_name$ = content

Multiline macro variable:
#def variable_name$
content line 1
content line 2
…
#end def

Inline macro function:
#def macro_name$(param1$; param2$;…) = content

Multiline macro function:
#def macro_name$(param1$; param2$;…)
content line 1
content line 2
…
#end def

Names of macro variables, macro functions, and their parameters can contain small and capital Latin letters and underscore "_". They must end with the "$" symbol. The contents can be virtually any string. It is not necessary to be a valid Calcpad expression, since it is not processed by the parser at this stage. However, other macro function/variable definitions are not allowed inside. You can insert only references to previously defined ones. Also, input fields "?" are not supported in macro functions yet. This feature will be developed in the next versions. You can use #include inside macro functions, but only if the included file does not contain other macro functions.

After a macro variable is defined, you can use it anywhere in the code by writing its name (with the ending "$"). The same is for macro functions, but you also need to specify values for parameters. Macro functions and macro variables are preprocessed and rewritten before the actual parsing is performed. As a result, intermediate (unwrapped) code is generated. You can review it by checking the "Unwrapped code" checkbox below the "Output" window.

If any errors occur during macro preprocessing, the unwrapped code is displayed, together with the errors. Line numbers in error descriptions refer to your initial code. If preprocessing is completed successfully, the unwrapped code is parsed and calculated as normal. If errors are detected at this stage, they are displayed in the output. Line numbers in error descriptions refer to the unwrapped code. You can go to the respective line by clicking the link on the line number.