<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Help understanding global variables vs local variables in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12130933#M26341</link>
    <description>&lt;P&gt;See &lt;A href="https://help.autodesk.com/view/ACD/2022/ENU/?guid=GUID-31B647C5-61F3-4B06-BC88-4CE83EB981C5" target="_blank" rel="noopener"&gt;THIS&lt;/A&gt; first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(defun addPoints (&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;l1 l2&lt;/STRONG&gt;&lt;/FONT&gt;)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;l1 and l2 are arguments! And from the link above:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;The symbols used as arguments are defined in the argument list before the local variables. &lt;STRONG&gt;Arguments are treated as a special type of local variable; argument variables are not available outside the function.&lt;/STRONG&gt; You cannot define a function with multiple arguments of the same name.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Jul 2023 13:51:32 GMT</pubDate>
    <dc:creator>ВeekeeCZ</dc:creator>
    <dc:date>2023-07-27T13:51:32Z</dc:date>
    <item>
      <title>Help understanding global variables vs local variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12130871#M26340</link>
      <description>&lt;P&gt;Hello, and thanks in advance for reading this. I'm a CAD drafter teaching himself a little AutoLISP between projects at work. I don't have any real coding experience prior to this, and am learning mostly by searching through documentation and using the blessed AfraLISP guides. I have already done some searching through these forums as well. Specifically,&amp;nbsp;&lt;A href="https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/local-and-global-variables/m-p/8270785" target="_blank" rel="noopener"&gt;this link back from 2018&lt;/A&gt;&amp;nbsp;didn't do much to help me, and only served to confuse me more.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm having some trouble understanding the difference between global and local variables. I thought a variable being global meant that if it was changed in a function call, it would stay changed if used again outside that function. And a local variable would be discarded after that function call was finished.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've got some code to explain my problem:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;(defun testMe ()
  
  (setq l1 (list 1 2)
        l2 (list 2 3)
  )
  
  (princ l2)
  
  (defun addPoints (l1 l2)
    ;Adds the values of two points together
    ;l1 = first point
    ;l2 = second point
    (setq a (+ (car l1) (car l2))
          b (+ (cadr l1) (cadr l2))
          l2 (list a b)
    )
  )
  
  (princ (addPoints l1 l2))
  (princ l2)
  (princ)
)&lt;/LI-CODE&gt;&lt;P&gt;addPoints just takes two points, and adds them together. To my understanding, l1 and l2 are global here (not defined following a / ). At the end of the addPoints function, l2 is set to the sum of the two points. Line 19 prints this to verify it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Because l2 is global, I would expect the value it is set inside of the addPoints function to be retained even after the addPoints function has ended. So after running (testMe), I should receive an output of (l2 prior to call)(l2 after call)(l2 after call), or (2 3)(3 5)(3 5).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I'm actually getting is (2 3)(3 5)(2 3). I can see that while l2 is being successfully called during the addPoints function, the value isn't being saved afterwards, despite the addPoints function passing the value to the (princ) command. This is how I'd expect the code to run if I had defined it as (addPoints ( / l1 l2)) So, I think I'm missing something about how global vs local variables work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't want to be rude or presumptuous, but I'd prefer responses that directly address my misunderstanding here, as opposed to asking why I didn't do this in a different way or offering another method (unless what I'm thinking is just completely wrong, of course).&amp;nbsp;&lt;EM&gt;In this exact example&lt;/EM&gt;, I'm sure there are plenty of workarounds and other ways to do this, but I'd like to be able to use functions to redefine more complicated items (like drawing elements) in the future, and being able to call (modifyElement(element value)) instead of having to step around with repeated (assoc)'s and (subst)'s would make my life a lot easier, and my code more readable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again for your time!&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 13:29:20 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12130871#M26340</guid>
      <dc:creator>Alex_Hughes6GQ7S</dc:creator>
      <dc:date>2023-07-27T13:29:20Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding global variables vs local variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12130933#M26341</link>
      <description>&lt;P&gt;See &lt;A href="https://help.autodesk.com/view/ACD/2022/ENU/?guid=GUID-31B647C5-61F3-4B06-BC88-4CE83EB981C5" target="_blank" rel="noopener"&gt;THIS&lt;/A&gt; first.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(defun addPoints (&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;l1 l2&lt;/STRONG&gt;&lt;/FONT&gt;)&amp;nbsp;&lt;/P&gt;
&lt;P&gt;l1 and l2 are arguments! And from the link above:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;The symbols used as arguments are defined in the argument list before the local variables. &lt;STRONG&gt;Arguments are treated as a special type of local variable; argument variables are not available outside the function.&lt;/STRONG&gt; You cannot define a function with multiple arguments of the same name.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 13:51:32 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12130933#M26341</guid>
      <dc:creator>ВeekeeCZ</dc:creator>
      <dc:date>2023-07-27T13:51:32Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding global variables vs local variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12131032#M26342</link>
      <description>&lt;P&gt;So if I wanted to change the start point of a line entity, I can't do something like this:&lt;/P&gt;&lt;LI-CODE lang="general"&gt;(defun moveLineStart (line destination)
  ;line = line to move
  ;destination = desired new coordinates of line start point
  (subst
    (cons (car destination) (cadr destination))
    (assoc 10 line)
    line
  )
)&lt;/LI-CODE&gt;&lt;P&gt;(I know entities aren't this easy to modify, but I think you understand what I'm trying to do here)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Instead I'd have to use the substitute chunk of code every time? That works, but it just adds a lot more text to the code, and I like to keep my code as short and easy to parse as I can&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 14:24:31 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12131032#M26342</guid>
      <dc:creator>Alex_Hughes6GQ7S</dc:creator>
      <dc:date>2023-07-27T14:24:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding global variables vs local variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12131068#M26343</link>
      <description>&lt;P&gt;The start point entry in entity data needs to start with a &lt;FONT color="#FF00FF"&gt;&lt;STRONG&gt;10&lt;/STRONG&gt;&lt;/FONT&gt;, but you can tack that right onto the front, without splitting the destination coordinates into separate entries.&amp;nbsp; And you then need to &lt;FONT face="courier new,courier" color="#0000FF"&gt;&lt;STRONG&gt;mod&lt;/STRONG&gt;&lt;/FONT&gt;ify that &lt;FONT face="courier new,courier" color="#0000FF"&gt;&lt;STRONG&gt;ent&lt;/STRONG&gt;&lt;/FONT&gt;ity to have its substituted entity data list take effect:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;(defun moveLineStart (line destination)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; ;line = &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;entity data list of&lt;/STRONG&gt;&lt;/FONT&gt; line to move &lt;FONT color="#FF0000"&gt;[not just entity name]&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; ;destination = desired new coordinates of line start point&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;(entmod&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; (subst&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;FONT color="#FF00FF"&gt;&lt;STRONG&gt;(cons 10 destination)&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; (assoc 10 line)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; line&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &amp;nbsp; ); subst&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;)&lt;/STRONG&gt;; entmod&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;)&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 14:55:04 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12131068#M26343</guid>
      <dc:creator>Kent1Cooper</dc:creator>
      <dc:date>2023-07-27T14:55:04Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding global variables vs local variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12131111#M26344</link>
      <description>&lt;P&gt;Sure you can. But for the arguments, I would prefer to use different names than the original variables.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;(defun moveLineStart (ent pnt / def)
  ;ent = line to move
  ;pnt  = desired new coordinates of line start point
  
  (setq def (entget ent))
  (entmod (subst (cons 10 pnt)
		 (assoc 10 def)
		 def))
  ent ; make sure it returns ename
  )&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Or more typically:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;(defun MainProgram ( / _moveLineStart _moveLineEnd)
  
  (defun _moveLineStart (d p)
    (subst (cons 10 p)   ;; subst returns definition
	   (assoc 10 d)
	   d))
  
  (defun _moveLineEnd (d p)
    (subst (cons 11 p)    ;; subst returns definition
	   (assoc 11 d)
	   d))
  
  ; --------------------
  
  (if (setq ent (car (entsel "\nSel the line: ")))
    (while (and
	     (setq pt1 (getpoint "\nNew beginning: "))
	     (setq pt2 (getpoint "\nNew end: "))
	     )
      (progn
	(setq def (entget ent))
	(setq def (_moveLineStart def pt1)) ; save the return of your sub
	(setq def (_moveLineEnd   def pt2))
	(entmod def)
))))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 14:45:51 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12131111#M26344</guid>
      <dc:creator>johnyDFFXO</dc:creator>
      <dc:date>2023-07-27T14:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding global variables vs local variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12131162#M26345</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://forums.autodesk.com/t5/user/viewprofilepage/user-id/14221183"&gt;@Alex_Hughes6GQ7S&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&lt;SPAN&gt;.... I'd have to use the substitute chunk of code every time? ... it just adds a lot more text to the code, and I like to keep my code as short and easy to parse as I can&lt;/SPAN&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;For that, consider alternative ways to achieve the same thing that may be available.&amp;nbsp; For example, in this case, if &lt;STRONG&gt;&lt;FONT face="courier new,courier"&gt;line&lt;/FONT&gt;&lt;/STRONG&gt; is an entity &lt;EM&gt;name&lt;/EM&gt;, not an entity data list:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;(defun moveLineStart (line destination)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;FONT size="2"&gt;;line = line &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;entity name&lt;/STRONG&gt; [not entity data list]&lt;/FONT&gt; to move&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;FONT size="2"&gt;;destination = desired new coordinates of line start point&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&amp;nbsp; &lt;STRONG&gt;&lt;FONT color="#0000FF"&gt;(setpropertyvalue line "StartPoint" destination)&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Jul 2023 15:01:55 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12131162#M26345</guid>
      <dc:creator>Kent1Cooper</dc:creator>
      <dc:date>2023-07-27T15:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding global variables vs local variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12132377#M26346</link>
      <description>&lt;P&gt;A couple of suggestions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;(setq pt3  (mapcar '+ pt1 pt2) )  ;  adds 2 points

(setq pt3 (mapcar '(lambda (x) (/ x 2.0)) pt3)  ;   divide by 2
(setq mp (mapcar '* (mapcar '+ p1 p3) '(0.5 0.5))) ; add &amp;amp; divide&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;You only need 1 function not 2 for lengthen, if you select the entity near an end can check which end you picked so for your task lengthen that end. An example.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="general"&gt;(defun swapends (ent / obj)
(setq pt3 (cadr ent))
(setq obj (vlax-ename-&amp;gt;vla-object (car ent)))
(setq start (vlax-curve-getstartPoint obj))
(setq end (vlax-curve-getEndPoint obj))
(setq d1 (distance pt3 end))
(setq d2 (distance pt3 start))
(if (&amp;lt; d1 d2)
(progn
(setq temp end)
(setq end start)
(setq start temp)
)
)
(princ)
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 28 Jul 2023 00:26:49 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/help-understanding-global-variables-vs-local-variables/m-p/12132377#M26346</guid>
      <dc:creator>Sea-Haven</dc:creator>
      <dc:date>2023-07-28T00:26:49Z</dc:date>
    </item>
  </channel>
</rss>

