Yes there is a difference. Localizing will "encapsulate" the variable(s) in the scope where it's made. E.g. look at the following:(defun PrintABC (heading / var)
(princ "\n\n")
(princ heading)
(princ "\n------------------------------------------------")
(foreach var '(a b c d)
(princ "\n\t")
(princ var)
(princ " = ")
(princ (eval var))
)
(princ)
)
(setq a "A global"
b "B global"
c "C global"
d "D global"
)
(defun testlocal (/ b d testlocal-sub)
(PrintABC "As testlocal starts")
(defun testlocal-sub ( / c d)
(PrintABC "As testlocal-sub starts")
(setq a "A in testlocal-sub"
b "B in testlocal-sub"
c "C in testlocal-sub"
d "D in testlocal-sub"
)
(PrintABC "As testlocal-sub finishes")
)
(setq a "A in testlocal"
b "B in testlocal"
c "C in testlocal"
d "D in testlocal"
)
(PrintABC "Just before testlocal-sub is called")
(testlocal-sub)
(PrintABC "As testlocal finishes")
)
(PrintABC "Just before testlocal is called")
(testlocal)
(PrintABC "After testlocal completes")
Running this code you get the following:Just before testlocal is called
------------------------------------------------
A = A global
B = B global
C = C global
D = D global
As testlocal starts
------------------------------------------------
A = A global
B = nil
C = C global
D = nil
Just before testlocal-sub is called
------------------------------------------------
A = A in testlocal
B = B in testlocal
C = C in testlocal
D = D in testlocal
As testlocal-sub starts
------------------------------------------------
A = A in testlocal
B = B in testlocal
C = nil
D = nil
As testlocal-sub finishes
------------------------------------------------
A = A in testlocal-sub
B = B in testlocal-sub
C = C in testlocal-sub
D = D in testlocal-sub
As testlocal finishes
------------------------------------------------
A = A in testlocal-sub
B = B in testlocal-sub
C = C in testlocal
D = D in testlocal
After testlocal completes
------------------------------------------------
A = A in testlocal-sub
B = B global
C = C in testlocal
D = D global
Firstly, all localized variables are initialized to nil. E.g. in the 2 cases where the defun starts you can see the localized versions contain nil
As you can see the b & d retained their original values at the end since they were localized in the testlocal function. It's as if inside the function there's new versions of b & d, and thus any setq's to them won't affect their previous versions.
Same happens inside the testlocal-sub, where c & d are localized. Only now you see that b gets altered for the localized version inside the testlocal function, thus its value in the "As testlocal finishes" position shows the value as it's been assigned in the testlocal-sub function.
If you simply nil'ed them all the globals would also be nil, as would all the values shown in "As testlocal finishes".
So here you can see the effect of nesting localizations. Sometimes you'd want to edit a value in the surrounding function, in such case you'd not localize the variable in the sub-function (as done with the b variable). Only when you want the value to be retained after all the defuns are complete would you NOT localize - as done with the a variable.