# 3.6.1 Defining and Displaying Macros

To define a new macro or to re-define an existing macro, use the define command. Since macros are defined by executing the define command, macros can be defined anywhere in Origin that allows LabTalk scripts to be executed. However, macros are generally defined in *.CNF files which get executed when Origin is started or in the Script window at run time.

The syntax for the define command is:

define macroName {script}

The define command defines a macro named macroName and associates it with a script {script}. MacroName can then be used like a command which, when executed, invokes the script.

This script defines a macro named hello that uses a loop to print a text string three times in the Script window:

def hello {
loop (ii, 1, 3) {
type "$(ii). Hello World"; }; }; To display a script that has been previously associated with a defined macro, use the define command without the {script} argument: define macroName For example: Executing the command define hello in the Script window, after having defined the hello macro as described above, will cause the following text to be printed in the Script window: define hello; { loop(ii, 1, 3) { type "$(ii). Hello World";
};
}

Macros can be passed up to five arguments. Use the  %1, %2, %3, %4, and %5 notation within the script definition to indicate that the macro expects one or more arguments. A macro can accept a number, string, variable, dataset, function, or even a script as an argument. Arguments to an Origin macro function similarly to MS-DOS batch arguments. An executing macro can access the number of arguments passed to it at run time via the macro.nArg object property.

This script defines a macro named goodbye that uses a loop to print out a goodbye message to the Script window for each argument string (up to five) it is passed.

The script uses the macro.nArg object property to determine the number of times the loop should iterate.

def goodbye {

%Z=%1 %2 %3 %4 %5;

for (ii = 1; ii <= macro.nArg; ii++) {

type "$(ii). Goodbye %[%Z,#$(ii)]";

};

};