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

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
490 Views
3 Replies
Message 1 of 4

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
Accepted solutions (1)
491 Views
3 Replies
Replies (3)
Message 2 of 4

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
Message 3 of 4

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
Message 4 of 4

oscar_mathieson
Observer
Observer

Perfect. Thank you!

0 Likes