Hi everyone,
I wonder if I can choose which the result exported from a sub program.
Something likes this:
(defun c:test () (setq a (subpro 5)) ) ; defun (defun subpro (b) (setq res1 (+ b 1)) (setq res2 (+ b 2)) )
The sub-program will have 2 results: 6 and 7. Howerver, I just want to use the first result (value 6) in the main program. So variable b will be assign to 6.
Great thanks.
Solved! Go to Solution.
Solved by Kent1Cooper. Go to Solution.
You can return the result from the subprogram as a list and then choose the 1st item or 2nd item in the list:
(defun subpro (b) (setq res1 (+ b 1)) (setq res2 (+ b 2))
(list res1 res2) )
Area Object Link | Attribute Modifier | Dwg Setup | Feet-Inch Calculator | Layer Apps | List on Steroids | VP Zoom Scales
Exchange App Store
Presumably you would sometimes want to use the second result from (subpro), or there's no point in having it there. So I think you need to include an argument for which result you want to use:
(defun test (var which)
(nth which (subpro var))
)
(defun subpro (a)
(list (+ a 1) (+ a 2))
)
Usage:
Your example situation:
Command: (test 5 0)
6
But if you want the other result:
Command: (test 5 1)
7
It could be made more intuitive to the non-Lisp-knowledgeable user, using 1 as the argument to get the first result, instead of zero:
(defun test (var which)
(nth (1- which) (subpro var))
)
An alternative method could be to use a form of indirect addressing by passing symbols as arguments to be assigned values by the subfunction, e.g.:
(defun c:test ( ) (subpro 5 'a 'b) ) (defun subpro ( arg out1 out2 ) (set out1 (+ arg 1)) (set out2 (+ arg 2)) nil )
_$ (c:test) nil _$ a 6 _$ b 7
Just ensure that the symbol supplied to the subfunction is not the same symbol used to represent the subfunction parameter (i.e. for the above example you could not evaluate the subfunction passing 'out1' or 'out2' as arguments), else the symbol will lose any value it is assigned following evaluation of the subfunction, since the scope of the parameter symbol is local to the function.