Jump to content

global local variable differences


steveR
 Share

Recommended Posts

Hello, I'm pretty new to scripting and programming so forgive me for asking a newbie question. When declaring variables, what are the differences between using the Dim or Local keywords or not using one at all, outside of functions, since they seem to have the same results? I know using Local inside functions limits the variable to that function. Also, what is the use of declaring a variable Global? If someone could write a small script so i could see the effects of using the different declarations, it would be much obliged. Thank you. :idiot:

AutoIt3 online docs Use it... Know it... Live it...MSDN libraryglobal Help and SupportWindows: Just another pane in the glass.
Link to comment
Share on other sites

I can show you an example of global... Global variables can only be edited from the place they were declared. They will remain the same throughout the script and cannot have their value reassigned. EX:

Global testing =1

testing = 5

This would not work because you cannot reassign testing unless you do it like this:

Global testing = 5

This would be valid because you are changing its value through the place where it was declared.

-I am the giver of life and the bringer of death.

Link to comment
Share on other sites

I can show you an example of global... Global variables can only be edited from the place they were declared. They will remain the same throughout the script and cannot have their value reassigned. EX:

Global testing =1

testing = 5

This would not work because you cannot reassign testing unless you do it like this:

Global testing = 5

This would be valid because you are changing its value through the place where it was declared.

<{POST_SNAPBACK}>

Everything you said is completely wrong. They are global, not constant. They can be changed either at the global scope after their initial declaration or inside a function.
Link to comment
Share on other sites

The difference between Dim/Local/Global is the scope in which they are created:

Dim = Local scope if the variable name doesn't already exist globally (in which case it reuses the global variable!)

Global = Forces creation of the variable in the Global scope

Local = Forces creation of the variable in the Local/Function scope

This is an interesting question.... It's really a good idea to explicitly declare variables with the "correct" scope to prevent programming bugs.

Outside of functions, Dim/Global/Local all have the same effect; so it doesn't really matter what you use. (In fact, you could probably omit the keyword entirely unless you are declaring an array.) On second thought, it could make a difference if you decide to #include that script inside of a function....

Inside of functions it makes a *huge* difference. I cannot tell you the number of times I had a bug because I forgot to declare Local $i to use in a stupid little For-loop .... The problem only arises when you are ALSO using $i as a variable outside your functions.

AutoIt allows you to create Global variables from within functions, but it's bad style to do that.

Hope that helps

Stupid example of forgetting to declare Local $i

; Display a rectangle of X's with height of 3 and width of 5
$rect = ""
For $i = 1 to 3
   $rect = $rect & DuplicateFiveTimes('X') & @CRLF
Next
MsgBox(4096,"Rectangle", $rect)
Exit

Func DuplicateFiveTimes($text)
   $output = ""
   For $i = 1 to 5
      $output = $output & $text
   Next
   Return $output
EndFunc
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
Link to comment
Share on other sites

Thanks for the responses guys. I haven't started learning to use #includes yet,but i think i understand scope a little better. Is it safe to say that declaring a variable Global allows it to be used throughout the script and functions including any #includes? Where declaring a variable Local limits it only to functions and/or the active script minus #includes? Thanks for all the help. :idiot:

AutoIt3 online docs Use it... Know it... Live it...MSDN libraryglobal Help and SupportWindows: Just another pane in the glass.
Link to comment
Share on other sites

  • Developers

Thanks for the responses guys. I haven't started learning to use #includes yet,but i think i understand scope a little better. Is it safe to say that declaring a variable Global allows it to be used throughout the script and functions including any #includes? Where declaring a variable Local limits it only to functions and/or the active script minus #includes? Thanks for all the help. :idiot:

<{POST_SNAPBACK}>

An #Include really only inserts a predefined piece of script code at its location. For ease of understanding you could imagine that the #include file is pasted in at that position. The big advantage of #include is that common functions are only coded once and included automatically in your script.

Local variables are limited to the function it is in.

Global will be known in everywhere, also inside #include(d) script portions..

the thing to realize here is that you #include can override the value of a Global variable when it uses the same variable name.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...