home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Current Shareware 1994 January
/
SHAR194.ISO
/
cad_util
/
alsps.zip
/
ALSP6.DOC
< prev
next >
Wrap
Lisp/Scheme
|
1993-11-04
|
6KB
|
211 lines
This is Lesson 6 of a series of AutoLISP training exercises given
on the CompuServe ADESK Forum by the Autodesk, Inc. Training
Department.
WRITING NEW AUTOCAD COMMANDS (Part Two)
GLOBAL AND LOCAL VARIABLES
There are two kinds of variables in AutoLISP. There are global
variables and local variables.
Global variables are declared at the top level of the AutoLISP
interpreter. Global variables and their values are accessible to any
expression, in or out of the body of a function definition. The
values may be retrieved. They may also be changed.
Changing the value of a global variable changes that value for all
expressions that access the variable in a global fashion.
For example, this expression binds the variable X to the value 1.
(setq x 1)
1
!x
1
Another expression that uses X will find its value to be 1.
(defun FINDX ()
(* x 1)
)
FINDX
(findx)
1
Placing the expression that binds X to a value inside a function
definition has exactly the same effect when the function is called,
that is, it globally changes the value of X.
(defun XGLOBAL ()
(setq x 2)
)
XGLOBAL
(xglobal)
2
!x
2
(findx)
2
A local variable differs from a global variable in the following ways.
1. It is declared local within a function definition.
2. Upon entering a function in which a variable has been locally
declared, AutoLISP will store the current value of the
variable.
3. Its value may change within the body of the function in which it
has been declared to be local, but as soon as the function
finishes executing, AutoLISP restores the original value of the
variable, or binds it to nil if it did not exist prior to its
declaration within the body of the function.
This has significant implications. Variables of the same name may be
freely used within applications without fear of conflict. Storage
space in the heap is optimized as variables return to their previous
value or to nil on exit from the function in which they are
locally bound.
A variable may be declared local to a function by placing it in the
list of required arguments and/or local variables in the function
definition.
For example, the variable X has been declared local in the following
function XLOCAL. But before you create XLOCAL, check to make certain
the current binding of the variable X is 2 (if it isn't, use SETQ to
bind it to 2).
!x
2
(defun XLOCAL (/ x)
(setq x 0)
)
XLOCAL
Notice the forward slash preceding the variable X. This is used to
separate the required arguments to a function from its
locally-declared variables.
Call the function XLOCAL in this form.
(xlocal)
0
As you would expect, XLOCAL returns the value of the last expression
in the body of the function definition, which is (setq x 0). X has
been bound to a value of 0 during execution of the function. But what
is its value now?
!x
2
The value of X has been restored to its value prior to entering the
function XLOCAL.
Add a PRINT statement to XLOCAL in order to examine the value of
X while still inside the function, and use PRIN1 to print a null
string on exit from the function. The PROMPT function generates
a string for the user to describe the variable that is being
printed.
(defun XLOCAL (/ x)
(setq x 0)
(prompt "The value of X is: ")
(print x)
(prin1)
)
XLOCAL
!x
2
(xlocal)
The value of X is:
0
!x
2
WHEN TO USE LOCAL VARIABLES
In general use, variables should be declared local within the
functions in which they are used. This avoids the possibility of name
conflict with variables of the same name elsewhere in the application
or within other applications, and can help reduce demands on memory
allocation.
However, variables should never be declared local until the
application has been fully debugged. The reason for this is simple.
If the application crashes or gives unpredictable results, it is
impossible to examine the values of locally-declared variables as they
were bound inside the calling function, because they return to their
previous state as soon as the function is terminated.
WHEN TO USE GLOBAL VARIABLES
Global variables should be used whenever more than one application or
function must determine the value of the same variable.
Next week: Writing New AutoCAD Commands, Part Three
Exercise
1. Rewrite the function MYFUN from Tutorial 5 so that the
variables PT1 and PT2 are declared local to the function.
2. Rewrite the function MYFUN from Tutorial 5 to require two
arguments. The arguments passed to MYFUN should evaluate to
point specifications.
Answers to Tutorial 5
1. A subr is the data type in AutoLISP for internal functions.
2. An expression whose first element is a function, constructed
with the intent of being evaluated.
3. Forms must have a function as their first element in order to
be evaluated. Lists constructed to hold data are not
necessarily evaluated, therefore, their first element need
not be a function.
4. A subr (or a user-defined function).
5. defun
6. The name of the function being defined and the list of
required arguments and/or local variables.
7. Expressions in the body of the definition that comprise the
processing instructions to be executed when the function is
called.
8. The value of the last expression in the body of the
definition.
9. Use the AutoLISP (load) function to load the file.
10. pause
11. Preface with C: the name of the function as it's defined by
(defun).
12. prin1
13. Include the name of a symbol in the list of required
arguments.
14. The values of any required arguments passed to a user-defined
function are mapped to all places in the expressions in the
body of the definition where the symbol named used in the
list of required arguments is also used.