乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 40|回复: 2

[编程交流] Local vs global variables - no

[复制链接]

31

主题

106

帖子

76

银币

初露锋芒

Rank: 3Rank: 3Rank: 3

铜币
154
发表于 2022-7-5 19:55:36 | 显示全部楼层 |阅读模式
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.
  1. (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:
  1. .; === Top statistic:; Function definition (with number of arguments): ((FOO . 0)).; === Top statistic:[color=red]; Global variables: (TEST)[/color]; 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?
回复

使用道具 举报

44

主题

3166

帖子

2803

银币

中流砥柱

Rank: 25

铜币
557
发表于 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
回复

使用道具 举报

114

主题

1万

帖子

1万

银币

中流砥柱

Rank: 25

铜币
543
发表于 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:
 
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-12 01:18 , Processed in 0.371708 second(s), 58 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表