Author Topic: A variable both as Local And Global  (Read 1354 times)

0 Members and 1 Guest are viewing this topic.

NirantarVidyarthee

  • Guest
A variable both as Local And Global
« on: January 20, 2013, 01:25:25 PM »
I am seeing an program that has the same variable used both global as well as local. Its working. So I am confused.

Example: (defun x / x)

What is the purpose of this? Why not use two different variable? Also won't they clash?


Lee Mac

  • Seagull
  • Posts: 12926
  • London, England
Re: A variable both as Local And Global
« Reply #1 on: January 20, 2013, 01:59:49 PM »
My guess is a typo when declaring local variables.

There is no clash as the argument symbol will hold the value passed when the function is called, e.g.:

Code: [Select]
_$ (defun f ( a / a ) (print a) (princ))
F
_$ (f 10)

10

However, you will receive a warning when checking the code using the VLIDE Check Utility (as described here).

NirantarVidyarthee

  • Guest
Re: A variable both as Local And Global
« Reply #2 on: January 20, 2013, 02:34:06 PM »
Thanks Lee,

It indeed gives an error in stats.

But since this is from code supplied by AutoDESK, I was just wondering if it was by design for a specific purpose.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: A variable both as Local And Global
« Reply #3 on: January 21, 2013, 01:58:08 AM »
No it's probably a typo from whoever wrote that lisp. It's definitely not helpful to have a local var clash with a parameter name. AutoLisp ignores the local var (as Lee's shown) - fortunately - if it was the other way round it would have been disastrous, at present it's only dangerous  :ugly: .

BTW, what you're referring to is not actually a "Global" variable. It's a parameter passed into a function. E.g.
Code: [Select]
(setq A 1)
(defun test (B / C)
  (setq C (+ B A)))
Note here A is a global, B is a parameter and C is a local.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.