DEFMACRO

Purpose:

Creates or defines a macro.

Syntax:

DEFMACRO("name", expr, quotes)

"name"

-

A string. The name of the macro, with optional arguments.

expr

-

Macro body. A string or any expression evaluating to a scalar.

quotes

-

Optional. An integer flag for the type of quote characters to be used for the macro body. If not specified, no quotes are used and a status message is displayed.

1:

The macro body is evaluated and used as is. The macro definition will be silent, i.e. status message is suppressed.

2:

Put single quotes around the macro body and cause the macro definition to be silent.

3:

Put double quotes around the macro body and cause the macro definition to be silent.

Example:

W1: 0..9

defmacro("m1", mean(W1))

 

creates a macro M1 containing the present mean value of W1, namely 4.5.

 

This construct is identical to:

 

#define m1 4.5

 

Because M1 is essentially a constant, M1 will not change if the data in W1 changes.

Example:

To create a similar macro where the value changes if W1 changes, use:

 

defmacro("m2", "mean(w1)")

 

This construct is identical to:

 

#define m2 mean(W1)

 

Now M2 returns the mean value of W1 and the value will change if the data in W1 changes.

Example:

To create a macro with arguments, the argument list must be included in the macro name, as in:

 

defmacro("mymac(a,b,c,d)","a*(b+max(c))+d")

 

W1: 0..9

W2: {1, 4, 7}

W3: mymac(2, 4, W1, W2)

 

W3 contains the series {27, 30, 33}.

 

Example:

defmacro("d1", getdate, 3)

 

returns the string "6-21-2013" if today's date is June 21, 2013.

Example:

defmacro("d1", getdate)

 

returns the integer -2028 if today's date is June 21, 2013. Unlike the previous example, since quotes are not used to enquote the result of evaluating getdate, we have the equivalent of:

 

#define d1 6-21-2013

 

So,

 

d1 == 6-21-2013 == -2028

Remarks:

DEFMACRO provides a method to create and store constants derived in a Worksheet. Macros are fully expanded before the statements are evaluated.

 

DEFMACRO always evaluates expr before assigning the result to the macro name. If a quote option is specified, the result of expr will be enclosed in quotes before assigning to the macro name.

 

If you redefine a macro and use it within the same statement, you must EVAL the macro before using it. For example: defmacro("x", 2); eval('x') + W1. Quotes determine how the macro is defined and interpreted, and what is returned after it is evaluated.

 

See SETVARIABLE to assign a variable.

 

DEFMACRO or #DEFINE is extremely useful for short, algorithmic abbreviations or constants. Use SPL to create more sophisticated functions or procedures.

 

The maximum length of a macro name is 63 characters.

See Also:

#DEFINE

CLEAR

EVAL

MACROS

SETVARIABLE