Announcements
Autodesk Community will be read-only between April 26 and April 27 as we complete essential maintenance. We will remove this banner once completed. Thanks for your understanding

Does setting a variable as local clear it to NIL or reset it to the value it had before running the routine?

oscar_mathieson
Observer

Does setting a variable as local clear it to NIL or reset it to the value it had before running the routine?

oscar_mathieson
Observer
Observer

What it says in the title. Just trying to reconcile some strange behavior of some variables.

0 Likes
Reply
Accepted solutions (1)
418 Views
3 Replies
Replies (3)

paullimapa
Mentor
Mentor
Accepted solution

If you declare a variable as local then the lisp function you've defined will not consider the variable if defined from an outside source nor impact the value defined but will only use the variable as defined in the current lisp function.

For example:

(setq a "THEY" b "THEM" c "US")

(defun test (/ a b c)(setq a "1" b "2" c "3")(princ(strcat"\na is [" a "] and b is [" b "] and c is [" c "]"))(princ))

values a b c will only be used locally with (test) function and will not impact what they're defined as outside the function:

paulli_apa_0-1671160197004.png

 


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos

Moshe-A
Mentor
Mentor

@oscar_mathieson hi,

 

to add what said @paullimapa 

 

the 'game' here is Scope. every variable (or function) has it's life time depending on where it is declared.

if you do not declare it locally, then it is global and every other function can reset it.

 

if a variable is declare locally, it's initial value is nil and it vanish (totally not exist anymore) as the function ends. a local variables 'lives' in autolisp memory separately from global variables. that means if a function declare a variable 'a' which already exist as global, they are not override each other.

 

same rule is for function arguments, their scope is for the duration of the function even if their names is the same as the variables outside (global or other).

 

Moshe

 

 

 

 

 

 

 

 

 

0 Likes

oscar_mathieson
Observer
Observer

Perfect. Thank you!

0 Likes