Example Using Local Variables

AutoCAD AutoLISP & Visual LISP

 
Example Using Local Variables
 
 
 

The following example shows the use of local variables in a user-defined function (be certain there is at least one space between the slash and the local variables).

_$ (defun LOCAL ( /
aaa bbb)
(_> (setq aaa "A" bbb
"B")
(_> (princ (strcat "\naaa
has the value " aaa ))
(_> (princ (strcat "\nbbb
has the value " bbb))
(_> (princ) )
LOCAL

Before you test the new function, assign variables aaa and bbb to values other than those used in the LOCAL function.

_$ (setq aaa 1 bbb 2)
2 

You can verify that the variables aaa and bbb are actually set to those values.

_$ aaa
1 
_$ bbb
2 

Now test the LOCAL function.

_$ (local)
aaa has the value A 
bbb has the value B 

You will notice the function used the values for aaa and bbb that are local to the function. You can verify that the current values for aaa and bbb are still set to their nonlocal values.

_$ aaa
1 
_$ bbb
2 

In addition to ensuring that variables are local to a particular function, this technique also ensures the memory used for those variables is available for other functions.