Local and global variables

Local and global variables

Anonymous
Not applicable
6,074 Views
3 Replies
Message 1 of 4

Local and global variables

Anonymous
Not applicable

Hello,

 

could someone explain me the difference between global and local variables, and one example for each one?

0 Likes
Accepted solutions (1)
6,075 Views
3 Replies
Replies (3)
Message 2 of 4

marko_ribar
Advisor
Advisor
Accepted solution

When you just type :

(setq var 1.0)

it is global variable and it's stored in CAD resident memory...

You can check it with :

!var

This should return : 1.0

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...

On the other hand - if you define function :

(defun foo ( / var )

  (setq var 1.0)

  (setq someothervar "string")

)

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...

Something like this :

(defun c:main ( / foo var rtn ) ; - here var is localized but it is lexical global to (foo)

  (defun foo ()

    ;; some stuff here

    (setq var 1.0)

  )

  (foo) ; - calling (foo) inside (c:main) - var becomes lexical global variable

  (setq rtn (1+ var)) ; - var is used after for correct operation of (c:main)

  (princ rtn)

) ; - end of (c:main)

Marko Ribar, d.i.a. (graduated engineer of architecture)
0 Likes
Message 3 of 4

Moshe-A
Mentor
Mentor

Hi,

 

the difference between local and global variables is scope.

 

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.

 

if at the command line you declare a variable: (setq FIRST-NAME "Casals") then 'FIRST-NAME' is global and is known to any expressions or functions in lisp scope (the same would be if you do this in the Visual Lisp Console window)

 

if you define a function: (defun gFunc ()  (setq A 10)) then (gFunc) is global and known to any expressions or functions in lisp scope. also 'A' is global.

 

 

now, if you do something like that:

(setq FULL-NAME "Jonny Cooper") ; global

 

(defun some-func (/ LAST-NAME FULL-NAME)

 (setq LAST-NAME "Jordi")

 (setq FULL-NAME (strcat FIRST-NAME " " LAST-NAME))

 (princ FULL-NAME)  ; print "Casals Jordi"

)

 

(princ FULL-NAME); print "Jonny Cooper"

 

(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.

FIRST-NAME was global and stay that after (some-func) is done.

 

in lisp global scope as long as you did not (setq) a variable it is bound value is nil.

as soon as you (setq) a variable then it is occupies some memory. if you want to free this memory

you set it to nil (most of us do not remember to release global variables Smiley LOL).

 

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.

 

function arguments:

(setq A 10 B 20)

 

(defun some-func (arg1 arg2)

  (princ arg1) ; print 10

  (princ arg2) ; princ 20

  (setq arg1 30 arg2 40) 

  (princ arg1); print 30

  (princ arg2); print 40

)

 

call function

(some-func A B)

 

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 & arg2 are new variables and only defined in function scope and automatically released when function is done.

 

(princ A); print 10

(princ B); print 20

 

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.

 

Moshe

 

 

 

 

 

0 Likes
Message 4 of 4

kerry_w_brown
Advisor
Advisor

Also try :

http://help.autodesk.com/view/ACD/2019/ENU/?guid=GUID-A8045F62-1CE3-4022-ACC1-CC0E2C1E158D

 

CaptureVariables.PNG


// Called Kerry or kdub in my other life.

Everything will work just as you expect it to, unless your expectations are incorrect. ~ kdub
Sometimes the question is more important than the answer. ~ kdub

NZST UTC+12 : class keyThumper<T> : Lazy<T>;      another  Swamper
0 Likes