# Help with Declarations

## Recommended Posts

As I'm new to this I'm studying as diligently as I can and going through the help file as much as possible, and doing searches and reading here on the forums.

But, for the question I'm about to ask the help file is just too vague on the subject and I can't seem to find any posts on my question specifically.(might actually be more than one)

I understand the purpose of declaring your variables. This is kind of obvious once you start using them.

The questions are these:

What exactly is the difference between Global, and Local?

The help file just said it was for the scope, but what scope? What is the global scope, and what is the local scope?

And how do I decide which variables must be declared and which don't really have to be?

I've seen a lot of scripts on here that use either global or local, and I've seen ones that don't declare every variable used even though there is a declaration of variables made.

And one more, I heard mention of a guide of some type and it sounded like a learners guide, something more in depth than the tutorials in the help file. If this is the case where can I find it? I can't seem to find a seperate download for anything like a learners guide.

##### Share on other sites

The basic explaination is.. Global variables can be used in any part of the script, the local only exists in the funcion where it were created..(sorry my bad english)

They are useful when you have to realize calculations and need to store temporary data for these calculations.

I don´t know any guide, if i found one i will link it in this topic.

Edited by monoscout999

##### Share on other sites

Example:

```\$g = 0
\$l = 0
MsgBox(1, "", "Global: " & \$g & @CRLF & "Local: " & \$l)

Global \$g = 0
Local \$l = 0
\$g += 1
\$l += 1
EndFunc```

##### Share on other sites

ok, so if I use a variable inside a function and declare it local, can I use the same variable (text name of the variable, not the same variable) inside a seperate function if I declare it as Local in each function? for instance

Func Action1()

Local \$var

\$var = 2

ConsoleWrite("\$var")

EndFunc

Func Action2()

Local \$var

\$var = 4

ConsoleWrite("\$var")

EndFunc

\$var would only = 2 when in Action(), and 4 only in Action2()

\$var being the text and having whatever expression or function for it not being the same would that be ok as long as I don't use \$var in a different part of the script without also declaring it there?

Edited by tigerray00

##### Share on other sites

Example:

```\$g = 0
\$l = 0
MsgBox(1, "", "Global: " & \$g & @CRLF & "Local: " & \$l)

Global \$g = 0
Local \$l = 0
\$g += 1
\$l += 1
EndFunc```

you have declared the global inside the local scope i think.

Autoit 123 is a good starting point if your just starting out

##### Share on other sites

sweet! thanks a bunch

##### Share on other sites

The local varable is destroyed after you exit the function.

##### Share on other sites

Ok, ty for the info everyone

Ok, so now I understand the scopes

So you'd only have to declare variables that actually require a declaration statement due to usage.

Edited by tigerray00

##### Share on other sites

Put #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 at the top of your script to see what variables aren't declared correctly.

## Create an account

Register a new account