Jump to content

Divide functions


svorgodne

Recommended Posts

Hello everyone,

 

Is there any solution to pass the value of a symbol defined in a function for another function to be used?

 

I made a very long routine that it is annoying to scroll down and up to find out mistakes, so I decided to divide it into several external functions that can be called by loading them where they would be needed.

 

At the end I wouldn't want to store any of these values, but I would like to keep them "alive" until the whole routine is over. That means pass some setq values to one function to the next one and so on.

 

The easiest way would be, of cours,e to put all the local variables in the main function for them not to be stored at the end of the whole process. I find it not so clean though.

 

Any suggestion? thanks in advance

Link to comment
Share on other sites

Something like this ?

 

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...