Explaining Local Variables
Local variables are known only to the current macro, or any macro called by the current macro. Variables that are not defined as local are automatically global. Global variables are visible to all macros. In general, you should localize all variables. Global variables can be dangerous and you should avoid using them.
To understand the difference between local and global variables, type the following three macros into a file. The three macros are very similar, so type the first macro and then use block copying for the other two. You can omit any comments.
DEFINE Outer_macro
{##############################################}
{## This macro shows the use of ##}
{## global variables ##}
{##############################################}
LET X 5 {initialize the variable "X"}
DISPLAY_NO_WAIT ('outer X = '+STR(X))
{display a message on the
command line }
WAIT 3 {allow time to read the message}
Middle_macro {call another macro }
DISPLAY_NO_WAIT ('outer X = '+STR(X))
WAIT 3
END_DEFINE
DEFINE Middle_macro
DISPLAY_NO_WAIT ('middle X = '+STR(X))
WAIT 3
Inner_macro
DISPLAY_NO_WAIT ('middle X = '+STR(X))
WAIT 3
END_DEFINE
DEFINE Inner_macro
DISPLAY_NO_WAIT ('inner X = '+STR(X))
WAIT 3
LET X 20
DISPLAY_NO_WAIT ('inner X = '+STR(X))
WAIT 3
END_DEFINE
Note the use of comments enclosed in braces {}. Anything enclosed in braces is ignored by the compiler. Comments help other readers understand your macro. If your macro is very complex, comments may help you understand it—especially six months later!
Your comments should explain the purpose of the command. The user can use the syntax diagrams to understand the command. A comment such as:
{allow time to read the message}
is more revealing than:
{wait approximately 3 seconds}
|
|
Comments cannot be nested. This means that if a section of code contains comments, you cannot "comment out" this code during debugging.
|
You can see that outer_macro calls middle_macro, which in turn calls inner_macro. This is an example of nested macros.
None of the macros use local variables, so the variable X is a global variable. This means that X is available to all three macros.
Input the file containing the three macros, then type on the command line:
Outer_macro
The command line output should be, in sequence:
'outer X = 5'
'middle X = 5'
'inner X = 5'
'inner X = 20'
'middle X = 20'
'outer X = 20'
Enter command
Before discussing global variables, let's have a quick look at some of the macro statements.
DISPLAY_NO_WAIT is used to display a message on the command line. After the message is displayed, no user action is required. This function differs from DISPLAY, which requires the user to press a key in order for the macro to continue.
DISPLAY_NO_WAIT will first print:
outer X =
What does STR(X) do? X is a NUMBER with some kind of internal machine representation using bits "set" or "cleared". Only ASCII characters can be displayed using the DISPLAY_NO_WAIT statement. STR(Age) converts the internal representation of X to an equivalent ASCII string, so that it can be displayed.
When used with strings, the + symbol concatenates the strings. For example, let's assume String1 is 'to', String2 is 'get', and String3 is 'her'. Then String1+String2+String3 is together. Any blanks inside the single quotes are also displayed.
WAIT 3 makes the system wait for approximately three seconds after the message is displayed. This gives you time to read the message.
The Middle_macro statement calls another macro that must be known to the system at run time. Since our three macros are all in the same file, the system can find Middle_macro when required.
Now, back to global variables. You can see that the value of X equal to 5, defined in Outer_macro, was known to the middle and inner macros. In Inner_macro, we changed the value of X to 20. This was known to the middle and outer macros.
Now add a line to the middle macro, after the DEFINE statement, as shown in the following example:
DEFINE Middle_macro
LOCAL X {added}
DISPLAY_NO_WAIT ('middle X = '+STR(X))
WAIT 3
Inner_macro
DISPLAY_NO_WAIT ('middle X = '+STR(X))
WAIT 3
END_DEFINE
Input the file, and type Outer_macro on the command line. The output should be:
'outer X = 5'
***The macro X is not defined
The system is indicating that it does not know the value of X. This is because X is now a local variable in Middle_macro. Other variables of the same name in any outer macro are not known to Middle_macro.
To make the macro run, add another line to the middle macro as shown:
DEFINE Middle_macro
LOCAL X
LET X 10 {added}
DISPLAY_NO_WAIT ('middle X = '+STR(X))
WAIT 3
Inner_macro
DISPLAY_NO_WAIT ('middle X = '+STR(X))
WAIT 3
END_DEFINE
Input the file, and type Outer_macro on the command line.
Now the output is:
'outer X = 5'
'middle X = 10'
'inner X = 10'
'inner X = 20'
'middle X = 20'
'outer X = 5'
Enter command
Here's what has happened:
• The LOCAL X statement in Middle_macro makes Middle_macro a watertight shell. Values of X cannot penetrate this shell. This means that values of X cannot be passed inwards from Outer_macro to Middle_macro, or outwards from Middle_macro to Outer_macro. But X can be passed inwards to any macro called by Middle_macro, unless any of these inner macros is also watertight to X.
• When X is set equal to 10 in Middle_macro, this value can then be passed to Inner_macro.
• When X is set equal to 20 in inner_macro, this value can be passed outwards to Middle_macro.
• Since X is a local variable in Middle_macro, Outer_macro does not know the value of X from Middle_macro. The value of X is the same value that was originally set in Outer_macro, which is 5.
Finally, let's make X a local variable in all three macros:
DEFINE Outer_macro
LOCAL X {added}
LET X 5
DISPLAY_NO_WAIT ('outer X = '+STR(X))
WAIT 3
Middle_macro
DISPLAY_NO_WAIT ('outer X = '+STR(X))
WAIT 3
END_DEFINE
DEFINE Middle_macro
LOCAL X
LET X 10
DISPLAY_NO_WAIT ('middle X = '+STR(X))
WAIT 3
Inner_macro
DISPLAY_NO_WAIT ('middle X = '+STR(X))
WAIT 3
END_DEFINE
DEFINE Inner_macro
LOCAL X {added}
LET X 15 {added}
DISPLAY_NO_WAIT ('inner X = '+STR(X))
WAIT 3
LET X 20
DISPLAY_NO_WAIT ('inner X = '+STR(X))
WAIT 3
END_DEFINE
Input the file and type Outer_macro on the command line. The output is follows:
'outer X = 5'
'middle X = 10'
'inner X = 15'
'inner X = 20'
'middle X = 10'
'outer X = 5'
Enter command
Do you understand the output?