Hi,
the difference between local and global variables is scope.
when you open a document in AutoCAD (NEW or OPEN) AutoLISP acquires some memory space, this is the scope for all lisp variables,expressions and functions.
if at the command line you declare a variable: (setq FIRST-NAME "Casals") then 'FIRST-NAME' is global and is known to any expressions or functions in lisp scope (the same would be if you do this in the Visual Lisp Console window)
if you define a function: (defun gFunc () (setq A 10)) then (gFunc) is global and known to any expressions or functions in lisp scope. also 'A' is global.
now, if you do something like that:
(setq FULL-NAME "Jonny Cooper") ; global
(defun some-func (/ LAST-NAME FULL-NAME)
(setq LAST-NAME "Jordi")
(setq FULL-NAME (strcat FIRST-NAME " " LAST-NAME))
(princ FULL-NAME) ; print "Casals Jordi"
)
(princ FULL-NAME); print "Jonny Cooper"
(some-func) is global, any variable declared after the '/' slash is defined as local, that means the scope of LAST-NAME and FULL-NAME is only known inside (some-func), as you can see global FULL-NAME is not harmed.
FIRST-NAME was global and stay that after (some-func) is done.
in lisp global scope as long as you did not (setq) a variable it is bound value is nil.
as soon as you (setq) a variable then it is occupies some memory. if you want to free this memory
you set it to nil (most of us do not remember to release global variables
).
in function scope a local variables also bound to nil. if you (setq) some local variables, after the function is done the variables are automatically released and there is no need set them to nil.
function arguments:
(setq A 10 B 20)
(defun some-func (arg1 arg2)
(princ arg1) ; print 10
(princ arg2) ; princ 20
(setq arg1 30 arg2 40)
(princ arg1); print 30
(princ arg2); print 40
)
call function
(some-func A B)
arg1 and arg2 are local arguments (also known as arguments by value) and why is that? you can set them inside the function scope and the value of the coming variables are not harmed. actually arg1 & arg2 are new variables and only defined in function scope and automatically released when function is done.
(princ A); print 10
(princ B); print 20
in AutoLISP there is also a way to sent variable to a function by reference (meaning if the arguments are set inside function scope, the new value is actually set to the coming variable) but that i'll tell you some other time.
Moshe