Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to choose the result from a sub program?

5 REPLIES 5
SOLVED
Reply
Message 1 of 6
maikhanhmst
332 Views, 5 Replies

How to choose the result from a sub program?

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.

Tags (1)
5 REPLIES 5
Message 2 of 6
paullimapa
in reply to: maikhanhmst

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


Paul Li
IT Specialist
@The Office
Apps & Publications | Video Demos
Message 3 of 6
Kent1Cooper
in reply to: maikhanhmst

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))

)

 

Kent Cooper, AIA
Message 4 of 6
maikhanhmst
in reply to: Kent1Cooper

Thanks all of you.
Message 5 of 6
Lee_Mac
in reply to: maikhanhmst

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.

Message 6 of 6
maikhanhmst
in reply to: Lee_Mac

Great. Thank you so much.

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost