vanowm 发表于 2022-7-5 19:55:36

Local vs global variables - no

Hello.
 
My understanding of local variable within one function is it should not be accessible within other functions. However it seems not true, if other functions were called within original function.

(defun foo (/ test)   (setq test "blah")   (foo2)   (princ))(defun foo2 ()   (print test))The result:
This opens quiet a few opportunities of sharing data within functions (sort of byRef), however it become more difficult to manage pollution of the script:
.; === Top statistic:; Function definition (with number of arguments): ((FOO . 0)).; === Top statistic:; Global variables: (TEST); Function definition (with number of arguments): ((FOO2 . 0)); Check done.Clearly the definition of "global" is stretch in this case, it's not truly global, now is it?

BlackBox 发表于 2022-7-5 20:53:52

You're neglecting to consider the scope of the calling function.
 
The nature of a local variable is not that it is only accessible from the calling function, and no others called within it's scope, but rather that local variables are returned to their original value for you following the end of a given Defun.
 
This nature is how one can successfully call *error* as local variable and not disrupt the OOTB native *error* handler, as one's custom defined *error* handler is merely processed within the scope of the calling function, and is then returned to its original definition (from startup).
 
Cheers

Lee Mac 发表于 2022-7-5 21:00:11

Below is a response to an email I received from a user requesting clarification on the importance of declaring local functions & variables (notably, declaring the *error* function as local), it may help with your understanding:
 
页: [1]
查看完整版本: Local vs global variables - no