Jump to content
Sign in to follow this  

Variable scopes - Test script

Recommended Posts

I wrote this up real quick to sort out some questions I had about AutoIt's variable scope. Did you know For loops within functions cannot use a Global as the iterator variable? I didn't.

Posting it here in case it can enlighten anybody else. It runs several tests, displaying the results of each test individually.

MsgBox(0, "Info", "global scope is when execution is NOT inside a function." & @CR & "local scope is any time execution IS inside a function.")
Global $globalglobal = 2
Func GlobalFunc1($value)
Local $globalglobal = $value
AssertError("'Local' in local scope will create a 'Local'", $globalglobal <> 2)
Global $globalset = 2
Func GlobalFunc2($value)
Global $globalset = $value
AssertError("'Global' in local scope creates/accesses a 'Global'", $globalset <> 10)
Dim $dimglobal = 2
Local $globallocal = 2
Func LocalGlobalFunc($value)
$dimglobal = $value
$globallocal = $value
AssertError("'Dim' or 'Local' in global scope will always create 'Global' anyway", ($dimglobal <> 10) Or ($globallocal <> 10))
For $globalindex = 1 to 4 ; $globalindex will be created as a Global
Local $junk = "something"
Global $lastglobalindex = $globalindex
Func GlobalLoop1($value)
Global $globalindex
$globalindex += $value
AssertError("loop indexes in global scope are always 'Global'", $globalindex <> ($lastglobalindex + 99))
Global $globalloop = 99
Func LoopFunc($last)
Global $globalloop ; This declaration should have no effect on the loop variable below
Local $firstgloballoop = $globalloop
For $globalloop = 1 to $last ; The index variable here will be Local notwithstanding our previous attempt to make it reference the existing Global
  Local $blah = "something"
Local $retarray[2]
$retarray[0] = $firstgloballoop
$retarray[1] = $globalloop
Return $retarray
Global $theretarray = LoopFunc(3)
AssertError("loop indexes in local scope will always be 'Local', even to the point of ignoring/overriding any prior 'Global' declarations", ($theretarray[0] <> 99) Or ($theretarray[1] <> 4))
Global $array[10]
$array[0] = "hi"
Func ArrayFunc($value)
Dim $array[5]
$array[0] = $value
AssertError("'Dim' in local scope will use a pre-existing 'Global' if it exists, otherwise create a 'Local'", $array[0] <> "lol")
MsgBox(0, "Done", "Thats the end of the tests!")
; Used to display test results only
Func AssertError($name, $bool)
If $bool Then Return MsgBox(16, "Test Results", '' & $name & ' = FAIL')
Return MsgBox(64, "Test Results", '' & $name & ' = SUCCESS')


Share this post

Link to post
Share on other sites


Did you know For loops within functions cannot use a Global as the iterator variable? I didn't

Then you should have read the Help file more carefully: :graduated:

variable - The variable used for the count.
The Variable will be created automatically with a LOCAL scope, even when MustDeclareVars is on.

And why would you want the actual count variable to be Global anyway - you reset it each time you start the loop! You can of course use a Global variable as the Start or Stop value - which actually serves a useful purpose.

Have you read the Variables - using Global, Local and ByRef tutorial in the Wiki - it explains scope in some detail. ;)


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:


ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area


Share this post

Link to post
Share on other sites

Then you should have read the Help file more carefully: :graduated:

Haha :) Barring some dramatically new syntax, I don't usually read exhaustively on every basic construct for a new language. (After all, most languages out there (that I use regularly) fall into two categories, C-like and BASIC-like.)

Good to know that it's in the manual though! I just found the script useful to illustrate the behavior (a little code can be faster than searching the manual sometimes ;) )

Edited by Unsigned


Share this post

Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...