Jump to content

Variable Localization in Sub-Routines.


Recommended Posts

Posted

Hi Guys,

 

Sorry for the plethora of Thread I've created today, but I have a sudden influx of questions that, if answered, would fill various holes in my knowledge of LISP.

 

My question is this:

 

If I have a subroutine, say for creating a list, and I set the list to a variable say "a", do I localise this variable in the sub-routine, and, if I do, will it still be available in the main routine?

 

i.e.

 

(defun c:test ()
   (testa)
   (alert a)
   (princ)
) ;_  end defun

(defun testa (/ [b][color=Red]a[/color][/b])
   (setq a (getstring t))
) ;_  end defun

 

Should I localise the highlighted variable, and, if I do, can I still use it in the main routine as shown?

 

Also, should I also localise it in the main routine?

Posted

(defun c:test2 (/ b)
 (setq b(testa))
   (alert b)
   (princ)
) ;_  end defun

 

or

 

(defun c:test3 ()
   (alert(testa))
   (princ)
) ;_  end defun

Posted

Thanks for your help ASMI, that is a bit of a work-around, and works well.

 

After a bit of experimentation, I have found that if you have a sub-routine with some variables that are then used in the main-routine, these variables should be localised in the main-routine and not the sub-routine for them to hold their values.

 

Otherwise, their values are set to nil after the sub-routine has completed.

Posted
After a bit of experimentation, I have found that if you have a sub-routine with some variables that are then used in the main-routine, these variables should be localised in the main-routine and not the sub-routine for them to hold their values.

 

Yes. Place subfunction inside main function to make it local (for LISP correctly to speak function).

 

(defun c:mainfun(/ [color="SeaGreen"][b]a[/b][/color])

 (defun subfun()
   (setq [color="SeaGreen"][b]a[/b][/color] ...)
 ); end subfun
...
(princ)
); end c:maifun

Posted
for LISP correctly to speak function...

 

 

Haha, I've really gotta learn the LISP lingo :P

 

 

Thanks ASMI, help appreciated as always :)

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