划分函数
大家好,有没有解决方案可以将函数中定义的符号的值传递给要使用的另一个函数?
我做了一个很长的例行程序,上下滚动查找错误很烦人,所以我决定将其分为几个外部函数,可以通过在需要的地方加载它们来调用。
最后,我不想存储这些值中的任何一个,但我想让它们“活着”,直到整个例程结束。这意味着将一些setq值传递给一个函数到下一个函数,以此类推。
当然,最简单的方法是将所有局部变量放在主函数中,这样它们就不会在整个过程结束时被存储。但我发现它不那么干净。
有什么建议吗?提前感谢 像这样的?
(defun C:SpaghettiCode ( / a b c a1 b1 c1 sub1 sub2 ) ; localise everything
;; declare some variables in our main function:
(mapcar 'set '(a b c) '(1 "this is b" t))
;; declare a subfunction that expects three variables, and asigns global symbols 'a1', 'b1' and 'c1'
(defun sub1 ( a b c )
(setq a1 (* a 2))
(setq b1 (strcat "yes, " b))
(setq c1 (if c :vlax-true :vlax-false))
)
;; declare a subfunction that uses the above global symbols 'a1', 'b1' and 'c1'
(defun sub2 nil
(alert
(strcat
"\n'a1' is: " (itoa a1)
"\n'b1' is: '" b1 "'"
"\n'c1' is: '" (vl-prin1-to-string c1) "'"
)
)
)
;; Run the subfunctions:
(sub1 a b c) ;; this will bound values to the symbols 'a1', 'b1' and 'c1'
(sub2) ;; this will use the symbols 'a1', 'b1' and 'c1'
(princ)
)
页:
[1]