Author Topic: Arguments and variables  (Read 259 times)

0 Members and 1 Guest are viewing this topic.

DanB

  • Bull Frog
  • Posts: 335
Arguments and variables
« on: January 10, 2019, 01:34:50 PM »
Looking for some guidance on how to make sure "aa" and "bb" are cleared once this is run. I've tried a few methods but not getting the desired results.

Code: [Select]
  (defun c:test1 ()
   (setq aa "This is test1...")
   (mytest)
  )
 
  (defun c:test2 ()
   (setq aa "This is test2...")
   (mytest)
  )
 
  (defun mytest (aa / bb)
   (setq bb 1)
   (prompt aa)
   (princ)
  )

Lee Mac

  • Seagull
  • Posts: 12225
  • London, England
Re: Arguments and variables
« Reply #1 on: January 10, 2019, 01:50:44 PM »
I would suggest the following:
Code - Auto/Visual Lisp: [Select]
  1. (defun c:test1 ( / aa )
  2.     (setq aa "This is test1...")
  3.     (mytest aa)
  4. )
  5. (defun c:test2 ( / aa )
  6.     (setq aa "This is test2...")
  7.     (mytest aa)
  8. )
  9. (defun mytest ( aa / bb )
  10.     (setq bb 1)
  11.     (prompt aa)
  12.     (princ)
  13. )

DanB

  • Bull Frog
  • Posts: 335
Re: Arguments and variables
« Reply #2 on: January 10, 2019, 02:00:34 PM »
Thank You. In line#3 and #7 you've added "aa". Is this then referred to as the argument of "mytest". That's the portion I was missing during my testing.

edit - I was focused solely on the first line of the defun in my attempts.

Lee Mac

  • Seagull
  • Posts: 12225
  • London, England
Re: Arguments and variables
« Reply #3 on: January 10, 2019, 02:06:42 PM »
Thank You. In line#3 and #7 you've added "aa". Is this then referred to as the argument of "mytest". That's the portion I was missing during my testing.

Exactly; in your code the variable 'aa' is global (i.e. residing in the document namespace) when defined within c:test1 and c:test2, whereas the parameter 'aa' required by your mytest function is local to the mytest function, and is assigned the value supplied when mytest is evaluated - in your code, you would have received a 'too few arguments' error.

In my code, the variable 'aa' is local to both c:test1 and c:test2 and the value of 'aa' is then passed to the function mytest and is assigned to the local parameter 'aa' in the mytest function.

DanB

  • Bull Frog
  • Posts: 335
Re: Arguments and variables
« Reply #4 on: January 10, 2019, 02:37:05 PM »
Thanks again. And yes, the too few arguments error was what threw me off.