home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / cad_util / alsps.zip / ALSP6.DOC < prev    next >
Lisp/Scheme  |  1993-11-04  |  6KB  |  211 lines

  1. This is Lesson 6 of a series of AutoLISP training exercises given
  2. on the CompuServe ADESK Forum by the Autodesk, Inc. Training
  3. Department.
  4.  
  5. WRITING NEW AUTOCAD COMMANDS (Part Two)
  6.  
  7. GLOBAL AND LOCAL VARIABLES
  8.  
  9. There are two kinds of variables in AutoLISP.  There are global
  10. variables and local variables.
  11.  
  12. Global variables are declared at the top level of the AutoLISP
  13. interpreter.  Global variables and their values are accessible to any
  14. expression, in or out of the body of a function definition.  The
  15. values may be retrieved.  They may also be changed.
  16.  
  17. Changing the value of a global variable changes that value for all
  18. expressions that access the variable in a global fashion.
  19.  
  20. For example, this expression binds the variable X to the value 1.
  21.  
  22.           (setq x 1)
  23.           1
  24.  
  25.           !x
  26.           1
  27.  
  28. Another expression that uses X will find its value to be 1.
  29.  
  30.           (defun FINDX ()
  31.             (* x 1)
  32.           )
  33.           FINDX
  34.  
  35.           (findx)
  36.           1
  37.  
  38. Placing the expression that binds X to a value inside a function
  39. definition has exactly the same effect when the function is called,
  40. that is, it globally changes the value of X.
  41.  
  42.           (defun XGLOBAL ()
  43.             (setq x 2)
  44.           )
  45.           XGLOBAL
  46.  
  47.           (xglobal)
  48.           2
  49.  
  50.           !x
  51.           2
  52.  
  53.           (findx)
  54.           2
  55.  
  56. A local variable differs from a global variable in the following ways.
  57.  
  58. 1.  It is declared local within a function definition.
  59.  
  60. 2.  Upon entering a function in which a variable has been locally
  61.     declared, AutoLISP will store the current value of the
  62.     variable.
  63.  
  64. 3.  Its value may change within the body of the function in which it
  65.     has been declared to be local, but as soon as the function
  66.     finishes executing, AutoLISP restores the original value of the
  67.     variable, or binds it to nil if it did not exist prior to its
  68.     declaration within the body of the function.
  69.  
  70. This has significant implications.  Variables of the same name may be
  71. freely used within applications without fear of conflict.  Storage
  72. space in the heap is optimized as variables return to their previous
  73. value or to nil on exit from the function in which they are
  74. locally bound.
  75.  
  76. A variable may be declared local to a function by placing it in the
  77. list of required arguments and/or local variables in the function
  78. definition.
  79.  
  80. For example, the variable X has been declared local in the following
  81. function XLOCAL.  But before you create XLOCAL, check to make certain
  82. the current binding of the variable X is 2 (if it isn't, use SETQ to
  83. bind it to 2).
  84.  
  85.           !x
  86.           2
  87.  
  88.           (defun XLOCAL (/ x)
  89.             (setq x 0)
  90.           )
  91.           XLOCAL
  92.  
  93. Notice the forward slash preceding the variable X.  This is used to
  94. separate the required arguments to a function from its
  95. locally-declared variables.
  96.  
  97. Call the function XLOCAL in this form.
  98.  
  99.           (xlocal)
  100.           0
  101.  
  102. As you would expect, XLOCAL returns the value of the last expression
  103. in the body of the function definition, which is (setq x 0).  X has
  104. been bound to a value of 0 during execution of the function.  But what
  105. is its value now?
  106.  
  107.           !x
  108.           2
  109.  
  110. The value of X has been restored to its value prior to entering the
  111. function XLOCAL.
  112.  
  113. Add a PRINT statement to XLOCAL in order to examine the value of
  114. X while still inside the function, and use PRIN1 to print a null
  115. string on exit from the function.  The PROMPT function generates
  116. a string for the user to describe the variable that is being
  117. printed.
  118.  
  119.           (defun XLOCAL (/ x)
  120.             (setq x 0)
  121.             (prompt "The value of X is: ")
  122.             (print x)
  123.             (prin1)
  124.           )
  125.           XLOCAL
  126.  
  127.           !x
  128.           2
  129.  
  130.           (xlocal)
  131.           The value of X is:
  132.           0
  133.  
  134.           !x
  135.           2
  136.  
  137. WHEN TO USE LOCAL VARIABLES
  138.  
  139. In general use, variables should be declared local within the
  140. functions in which they are used.  This avoids the possibility of name
  141. conflict with variables of the same name elsewhere in the application
  142. or within other applications, and can help reduce demands on memory
  143. allocation.
  144.  
  145. However, variables should never be declared local until the
  146. application has been fully debugged.  The reason for this is simple.
  147. If the application crashes or gives unpredictable results, it is
  148. impossible to examine the values of locally-declared variables as they
  149. were bound inside the calling function, because they return to their
  150. previous state as soon as the function is terminated.
  151.  
  152. WHEN TO USE GLOBAL VARIABLES
  153.  
  154. Global variables should be used whenever more than one application or
  155. function must determine the value of the same variable.
  156.  
  157. Next week:  Writing New AutoCAD Commands, Part Three
  158.  
  159. Exercise
  160.  
  161. 1.  Rewrite the function MYFUN from Tutorial 5 so that the
  162.     variables PT1 and PT2 are declared local to the function.
  163.  
  164. 2.  Rewrite the function MYFUN from Tutorial 5 to require two
  165.     arguments.  The arguments passed to MYFUN should evaluate to
  166.     point specifications.
  167.  
  168. Answers to Tutorial 5
  169.  
  170. 1.  A subr is the data type in AutoLISP for internal functions.
  171.  
  172. 2.  An expression whose first element is a function, constructed
  173.     with the intent of being evaluated.
  174.  
  175. 3.  Forms must have a function as their first element in order to
  176.     be evaluated.  Lists constructed to hold data are not
  177.     necessarily evaluated, therefore, their first element need
  178.     not be a function.
  179.  
  180. 4.  A subr (or a user-defined function).
  181.  
  182. 5.  defun
  183.  
  184. 6.  The name of the function being defined and the list of
  185.     required arguments and/or local variables.
  186.  
  187. 7.  Expressions in the body of the definition that comprise the
  188.     processing instructions to be executed when the function is
  189.     called.
  190.  
  191. 8.  The value of the last expression in the body of the
  192.     definition.
  193.  
  194. 9.  Use the AutoLISP (load) function to load the file.
  195.  
  196. 10. pause
  197.  
  198. 11. Preface with C: the name of the function as it's defined by
  199.     (defun).
  200.  
  201. 12. prin1
  202.  
  203. 13. Include the name of a symbol in the list of required
  204.     arguments.
  205.  
  206. 14. The values of any required arguments passed to a user-defined
  207.     function are mapped to all places in the expressions in the
  208.     body of the definition where the symbol named used in the
  209.     list of required arguments is also used.
  210.  
  211.