<?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: Local and global variables in Visual LISP, AutoLISP and General Customization Forum</title>
    <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/local-and-global-variables/m-p/8271650#M99470</link>
    <description>&lt;P&gt;Also try :&lt;/P&gt;&lt;P&gt;&lt;A href="http://help.autodesk.com/view/ACD/2019/ENU/?guid=GUID-A8045F62-1CE3-4022-ACC1-CC0E2C1E158D" target="_blank"&gt;http://help.autodesk.com/view/ACD/2019/ENU/?guid=GUID-A8045F62-1CE3-4022-ACC1-CC0E2C1E158D&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CaptureVariables.PNG" style="width: 972px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/546817i266BE8C2692A4BD7/image-size/large?v=v2&amp;amp;px=999" role="button" title="CaptureVariables.PNG" alt="CaptureVariables.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 17 Sep 2018 06:52:50 GMT</pubDate>
    <dc:creator>kerry_w_brown</dc:creator>
    <dc:date>2018-09-17T06:52:50Z</dc:date>
    <item>
      <title>Local and global variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/local-and-global-variables/m-p/8270785#M99467</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;could someone explain me the difference between global and local variables, and one example for each one?&lt;/P&gt;</description>
      <pubDate>Sun, 16 Sep 2018 10:46:39 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/local-and-global-variables/m-p/8270785#M99467</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2018-09-16T10:46:39Z</dc:date>
    </item>
    <item>
      <title>Re: Local and global variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/local-and-global-variables/m-p/8270802#M99468</link>
      <description>&lt;P&gt;When you just type :&lt;/P&gt;&lt;P&gt;(setq var 1.0)&lt;/P&gt;&lt;P&gt;it is global variable and it's stored in CAD resident memory...&lt;/P&gt;&lt;P&gt;You can check it with :&lt;/P&gt;&lt;P&gt;!var&lt;/P&gt;&lt;P&gt;This should return : 1.0&lt;/P&gt;&lt;P&gt;You can store 4 types of data into variables : reals, integers, strings or lists - lists can contain sub lists and they can have all those 4 types as elements...&lt;/P&gt;&lt;P&gt;On the other hand - if you define function :&lt;/P&gt;&lt;P&gt;(defun foo ( / var )&lt;/P&gt;&lt;P&gt;&amp;nbsp; (setq var 1.0)&lt;/P&gt;&lt;P&gt;&amp;nbsp; (setq someothervar "string")&lt;/P&gt;&lt;P&gt;)&lt;/P&gt;&lt;P&gt;Here variable var is local to the function and is stored in local foo function memory space... It would become global if it wasn't localized after foo - function name with ( / var ) - localization of var variable... So someothervar variable is after execution of (foo) stored in CAD resident memory like it was set as global, but only after execution of (foo)... So technically speaking someothervar is local to foo function, but it isn't localized and may occur as global after calling (foo), so watch out to localize all defined variables from defined function, not to have interference with maybe some other resident variables, so that when calling some other defined function global variables don't mess correct operation of that other function that was called... On the other hand sometime you need not to localize some for you useful variable that would become lexical global for parent defined function - main function where then is preferable to localize that lexical global to that main function and leave it not localized in that child function from where it was defined and after called that child function inside main function - that variable becomes lexical global and is used after calling child function for correct functionality of main function in operations of main function that are performed after calling child function...&lt;/P&gt;&lt;P&gt;Something like this :&lt;/P&gt;&lt;P&gt;(defun c:main ( / foo var rtn ) ; - here var is localized but it is lexical global to (foo)&lt;/P&gt;&lt;P&gt;&amp;nbsp; (defun foo ()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ;; some stuff here&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; (setq var 1.0)&lt;/P&gt;&lt;P&gt;&amp;nbsp; )&lt;/P&gt;&lt;P&gt;&amp;nbsp; (foo) ; - calling (foo) inside (c:main) - var becomes lexical global variable&lt;/P&gt;&lt;P&gt;&amp;nbsp; (setq rtn (1+ var)) ; - var is used after for correct operation of (c:main)&lt;/P&gt;&lt;P&gt;&amp;nbsp; (princ rtn)&lt;/P&gt;&lt;P&gt;) ; - end of (c:main)&lt;/P&gt;</description>
      <pubDate>Sun, 16 Sep 2018 11:30:33 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/local-and-global-variables/m-p/8270802#M99468</guid>
      <dc:creator>marko_ribar</dc:creator>
      <dc:date>2018-09-16T11:30:33Z</dc:date>
    </item>
    <item>
      <title>Re: Local and global variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/local-and-global-variables/m-p/8271116#M99469</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the difference between local and global variables is&amp;nbsp;&lt;EM&gt;scope.&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when you open a document in AutoCAD (NEW or OPEN) AutoLISP acquires some memory space, this is the scope for all lisp variables,expressions and functions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if at the command line you declare a variable: (setq FIRST-NAME "Casals") then '&lt;SPAN&gt;FIRST-NAME&lt;/SPAN&gt;' is global and is known to any expressions or functions&amp;nbsp;in lisp scope (the same would be if you do this in the Visual Lisp Console window)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if you define a function: (defun gFunc ()&amp;nbsp; (setq A 10)) then (gFunc) is global and known to any expressions or functions in lisp scope. also 'A' is global.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;now, if you do something like that:&lt;/P&gt;&lt;P&gt;(setq FULL-NAME "Jonny Cooper") ; global&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(defun some-func (/ LAST-NAME FULL-NAME)&lt;/P&gt;&lt;P&gt;&amp;nbsp;(setq LAST-NAME "Jordi")&lt;/P&gt;&lt;P&gt;&amp;nbsp;(setq FULL-NAME (strcat FIRST-NAME " " LAST-NAME))&lt;/P&gt;&lt;P&gt;&amp;nbsp;(princ FULL-NAME)&amp;nbsp; ; print&amp;nbsp;&lt;SPAN&gt;"Casals Jordi"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(princ&amp;nbsp;&lt;SPAN&gt;FULL-NAME); print "Jonny Cooper"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(some-func) is global, any variable declared after the '/' slash is defined as local, that means the scope of LAST-NAME and FULL-NAME is only known inside (some-func), as you can see global FULL-NAME is not harmed.&lt;/P&gt;&lt;P&gt;FIRST-NAME was global and stay that after (some-func) is done.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in lisp global scope&amp;nbsp;as long as you did not (setq) a variable it is bound value is nil.&lt;/P&gt;&lt;P&gt;as soon as you (setq) a variable then it is occupies some memory. if you want to free this memory&lt;/P&gt;&lt;P&gt;you set it to nil (most of us do not remember to release global variables &lt;img id="smileylol" class="emoticon emoticon-smileylol" src="https://forums.autodesk.com/i/smilies/16x16_smiley-lol.png" alt="Smiley LOL" title="Smiley LOL" /&gt;).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in function scope a local variables also bound to nil. if you (setq) some local variables, after the function is done the variables are automatically released and there is no need set them to nil.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;function arguments:&lt;/P&gt;&lt;P&gt;(setq A 10 B 20)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(defun some-func (arg1 arg2)&lt;/P&gt;&lt;P&gt;&amp;nbsp; (princ arg1) ; print 10&lt;/P&gt;&lt;P&gt;&amp;nbsp; (princ arg2) ; princ 20&lt;/P&gt;&lt;P&gt;&amp;nbsp; (setq arg1 30 arg2 40)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; (princ arg1); print 30&lt;/P&gt;&lt;P&gt;&amp;nbsp; (princ arg2); print 40&lt;/P&gt;&lt;P&gt;)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;call function&lt;/P&gt;&lt;P&gt;(some-func A B)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;arg1 and arg2 are local arguments (also known as arguments by value) and why is that? you can set them inside the function scope and the value of the coming variables are not harmed. actually arg1 &amp;amp; arg2 are new variables and only defined in function scope and automatically released when function is done.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;(princ A); print 10&lt;/P&gt;&lt;P&gt;(princ B); print 20&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in AutoLISP there is also a way to sent variable to a function by reference (meaning if the arguments are set inside function scope, the new value is actually set to the coming variable) but that i'll tell you some other time.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Moshe&lt;/P&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;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 16 Sep 2018 18:36:28 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/local-and-global-variables/m-p/8271116#M99469</guid>
      <dc:creator>Moshe-A</dc:creator>
      <dc:date>2018-09-16T18:36:28Z</dc:date>
    </item>
    <item>
      <title>Re: Local and global variables</title>
      <link>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/local-and-global-variables/m-p/8271650#M99470</link>
      <description>&lt;P&gt;Also try :&lt;/P&gt;&lt;P&gt;&lt;A href="http://help.autodesk.com/view/ACD/2019/ENU/?guid=GUID-A8045F62-1CE3-4022-ACC1-CC0E2C1E158D" target="_blank"&gt;http://help.autodesk.com/view/ACD/2019/ENU/?guid=GUID-A8045F62-1CE3-4022-ACC1-CC0E2C1E158D&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CaptureVariables.PNG" style="width: 972px;"&gt;&lt;img src="https://forums.autodesk.com/t5/image/serverpage/image-id/546817i266BE8C2692A4BD7/image-size/large?v=v2&amp;amp;px=999" role="button" title="CaptureVariables.PNG" alt="CaptureVariables.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Sep 2018 06:52:50 GMT</pubDate>
      <guid>https://forums.autodesk.com/t5/visual-lisp-autolisp-and-general/local-and-global-variables/m-p/8271650#M99470</guid>
      <dc:creator>kerry_w_brown</dc:creator>
      <dc:date>2018-09-17T06:52:50Z</dc:date>
    </item>
  </channel>
</rss>

