Author Topic: Local vars etc.  (Read 5019 times)

0 Members and 1 Guest are viewing this topic.

SMadsen

  • Guest
Local vars etc.
« on: January 23, 2004, 08:28:06 AM »
Interesting use of scope
(defun open_file -- (/ ---> open_file)
.. (setq open_file (... ))

This would throw a VBA'er off :)

Carry on ...

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Local vars etc.
« Reply #1 on: January 23, 2004, 08:38:32 AM »
Nah .... just have to take a second look at it ...
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Mark

  • Custom Title
  • Seagull
  • Posts: 28762
Local vars etc.
« Reply #2 on: January 23, 2004, 08:41:41 AM »
It's funny how many times you look at something and not see those types of things! Thanks Stig.
TheSwamp.org  (serving the CAD community since 2003)

SMadsen

  • Guest
Local vars etc.
« Reply #3 on: January 23, 2004, 09:15:23 AM »
Quote from: Keith
Nah .... just have to take a second look at it ...

Well, if VBA was all I knew (umm, don't even know it, just pretending) I would scratch my head when knowing I'd get a compile error on

Code: [Select]
Private Function sum(num1 As Double, num2 As Double) As Double
  Dim sum As Double
  sum = 200.3
  sum = sum + num1 + num2
End Function


In lisp, there's nothing wrong with using the func name as a local var.

deegeecees

  • Guest
Local vars etc.
« Reply #4 on: January 23, 2004, 09:34:09 AM »
I never knew you could use a function name as a way to store a variable. Could someone please explain how this works? I've been using lisp for 12 years now and I'm still a newbie!  :oops:  I haven't even looked at VLisp yet.

SMadsen

  • Guest
Local vars etc.
« Reply #5 on: January 23, 2004, 09:39:26 AM »
As not to pollute Mark's thread further, I'll be taking comments to a new thread.

deegeecees

  • Guest
Local vars etc.
« Reply #6 on: January 23, 2004, 09:54:50 AM »
Thanks Stig :)

SMadsen

  • Guest
Local vars etc.
« Reply #7 on: January 23, 2004, 12:47:29 PM »
Quote from: SMadsen
In lisp, there's nothing wrong with using the func name as a local var.

deegeecees, the above statement is not exactly true in all instances. We'll get back to that.

Quote from: deegeecees
I never knew you could use a function name as a way to store a variable.

You can't. Well, you can but then it won't be a function anymore (except if the variable is a SUBR, EXRXSUBR or USUBR, of course).

AutoLISP creates something called stack frames for each function call. A frame consists of the function call and the parameters such as arguments and local variables. While a frame exists, you can assign values to parameters but you can't alter the function call itself as it "owns" the frame.

For example, the function below will put a frame on the stack and assign values to the local variable:

(defun test (num / test)(setq test (1+ (setq test num))))

Try enter it in the VLIDE and set a breakpoint at the opening paren of the first SETQ. Run it with (test 1.0). When halted, try invoke View->Trace Stack. You'll see the frame (TEST 1.0) appear with a level number in square brackets. Right clicking on it will give you the option of inspecting the parameters. Notice that the local var TEST is nil (or a number depending if you stepped through the code) - it doesn't point to the function.


However, if we force AutoLISP to create yet another frame by calling the function recursively, we will have messed it up by using the function name as a local var:

(defun test (num / test) (setq test (test (1+ num))))

Set the breakpoint the same place, call it with the expression before and step through it until you get an error. Then open the Trace Stack again (or click the refresh button if it's already open). You will notice two frames on the stack:

[2](TEST 2.0)
[3](TEST 1.0)

While we couldn't alter the function call within a single frame, it is certainly possibly to alter it between frames. Because TEST calls itself and thereby needs a new frame on the stack, it will now see the value of the local variable with the same name as being the function address and say, "hey, that's not a valid address!".

So, there IS something wrong with using the function name as a local variable (shame on me) but, in the scope of a function call, the two are completely different entities.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Local vars etc.
« Reply #8 on: January 23, 2004, 01:18:27 PM »
Oh Stig, that brings to mind something ...

In VBA you can't do this:
Code: [Select]

Private Function sum(num1 As Double, num2 As Double) As Double
  Dim sum As Double
  sum = 200.3
  sum = sum + num1 + num2
End Function


You CAN do this ....

Code: [Select]

Private Function sum(num1 As Double, num2 As Double) As Double
  sum = 200.3
  sum = sum + num1 + num2
End Function


Just don't seem right now does it :)

DCG, I think what Stig is referring to is this scenario..
Code: [Select]

(defun commandname ( / commandname )
 (do stuff here) ;commandname function is not affected
)


This is perfectly OK to do as the global var function commandname is put into the stack and the local var commandname is utilized until the function evaluation is completed, then the global value becomes accessable once again.

See what you learn here .....
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie