WRP Posted April 2, 2016 Share Posted April 2, 2016 A variable declared within a function is automatically Local. Anything declared outside a function is automatically Global, even if it was declared Local, since there is no file scope. As for declaring Global within a function, that's just a bad idea. So is there really any benefit to having the Local/Global declarations? Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 2, 2016 Moderators Share Posted April 2, 2016 WRP, I believe so - declaring the scope implicitly makes sure that the coder fully realises the scope for that particular variable/constant. M23 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: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
WRP Posted April 2, 2016 Author Share Posted April 2, 2016 I have tried writing scripts without Local/Global and everything seems to work fine. I would expect there to be problem situations, though, since the single equals sign is overloaded as both assignment and test. Can you think of example cases where omitting Local/Global would break the code? Link to comment Share on other sites More sharing options...
jchd Posted April 3, 2016 Share Posted April 3, 2016 Plenty! This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
czardas Posted April 3, 2016 Share Posted April 3, 2016 (edited) To prevent crude, unsophisticated and extremely limited functionality; we use scoping. The following is a crude demonstration of a local / global collision. Sure it's possible to write code like this, but once you start working on larger projects you don't want to have to keep tabs on all the possible collisions. $i = 0 ConsoleWrite(blah(blah(blah(''))) & $i & @LF) Func blah($_) $i = 1 EndFunc The best way to avoid collisions is to always try to use local variables. Sometimes achieving this can present a bit of a challenge; but it's a very flexible, and certainly the safest, approach. So now we can write this: Local $i = 0 ConsoleWrite(blah(blah(blah(''))) & $i & @LF) Func blah($_) Local $i = 1 EndFunc Both the variable declarations in the above example are correct, but there is a potential problem. The first (local) declaration of $i produces a global variable by default, because it was not declared within the function blah(). The second declaration produces a local variable because it was declared as a local variable within the function blah(). To ease the mind (but not necessarily the conscience), we can make the code easier to understand by being more explicit and use the keyword Global for the first declaration. Doing so doesn't change the outcome or reduce the risk of a collision. Global $i = 0 ConsoleWrite(blah(blah(blah(''))) & $i & @LF) Func blah($_) Local $i = 1 EndFunc The above code is not a good example because it is quite meaningless and does not actually need to use any variables at all. ConsoleWrite(blah(blah(blah(''))) & 0 & @LF) Func blah($_) EndFunc So why use global variables? One example of good usage is when you want to share data between several scripts (and other developers). Perhaps the data is a constant like Pi: two letters being easier to remember than a larger number of digits. In all cases, using global variables runs the risk of encountering collisions. Using long and unusual variable names reduces the likelihood of a collision but makes the code harder to read. All considerations / trade-offs need to be kept in perspective. Explicitly declaring local variables within functions prevents collisions, which I assume is the main objective. Edited April 3, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
philkryder Posted April 3, 2016 Share Posted April 3, 2016 does anyone use g,p, and l to distinguish and avoid collisions? $igCounter for a global integer variable $ilCounter for a local integer $ipCounter for a parameter? Link to comment Share on other sites More sharing options...
czardas Posted April 3, 2016 Share Posted April 3, 2016 Take a look here: https://www.autoitscript.com/wiki/Best_coding_practices#Scopes_of_Variables philkryder 1 operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
RTFC Posted April 3, 2016 Share Posted April 3, 2016 (edited) 13 hours ago, WRP said: Anything declared outside a function is automatically Global, even if it was declared Local, since there is no file scope. There's a caveat to this (incidentally, one that is not mentioned in that Best coding practices that czardas just linked to), in that a For variable inside a function with the same name as an existing global variable is treated as a different, purely local variable. This will be more obvious when explicitly (re)declared. Example: Global $myGlobalVar=123 Consolewrite("value before call " & $myGlobalVar & @CRLF) _MyFunc() Consolewrite("value after call " & $myGlobalVar & @CRLF) Func _MyFunc() ;Global $myGlobalVar ; even if explicitly declared as global here, it's still redeclared local by For For $myGlobalVar=1 to 3 consolewrite("value in func " & $myGlobalVar & @CRLF) Next EndFunc So usage of For <var>=x to y inside a function also makes that var Local, even if it has previously been declared as having global scope, and even if it is redeclared as Global inside that function beforehand. I ran into this when I tried to use a For loop inside a function to try and adjust a global variable's value, which turned out to be a terrible idea. So all the more reason to always explicitly declare, if only to keep conscious track of scope (and make the code easier to read for your future self, and others). Edited April 3, 2016 by RTFC My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
czardas Posted April 3, 2016 Share Posted April 3, 2016 (edited) @RTFC LOL, just imagine if a local loop iteration count (within a function) was able to corrupt a global variable in that way: total nightmare. Edited April 3, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
RTFC Posted April 3, 2016 Share Posted April 3, 2016 (edited) @czardas: Yeah, it wasn't a particularly bright design concept on my part, I agree. But I figured that an explicit global declaration in <main> would always trump any explicitly undeclared var's scope inside a func. My bad, of course. Edited April 3, 2016 by RTFC My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
czardas Posted April 3, 2016 Share Posted April 3, 2016 (edited) Just to let you know, I tested it too and was quite relieved after confirming the true behaviour. Edited April 3, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
RTFC Posted April 3, 2016 Share Posted April 3, 2016 The Help file text of the For statement does mention it, but of course I didn't read up on it, I just tried and watched it fail horrendously. And not having explicitly declared the var local in the func, I assumed it would remain Global there (NOT). I'm just glad I don't work for NASA... czardas and mLipok 2 My Contributions and Wrappers Spoiler BitMaskSudokuSolver BuildPartitionTable CodeCrypter CodeScanner DigitalDisplay Eigen4AutoIt FAT Suite HighMem MetaCodeFileLibrary OSgrid Pool RdRand SecondDesktop SimulatedAnnealing Xbase I/O Link to comment Share on other sites More sharing options...
WRP Posted April 3, 2016 Author Share Posted April 3, 2016 11 hours ago, czardas said: Explicitly declaring local variables within functions prevents collisions... I understand the argument of using Local/Global for code readability, but I don't see it for avoiding collisions. As far as I can tell, except for the case of Global within a function, AutoIt assigns the same scope to variables whether or not Local/Global is used. So, Local should have absolutely no effect wherever it is used. Link to comment Share on other sites More sharing options...
trancexx Posted April 3, 2016 Share Posted April 3, 2016 (edited) 26 minutes ago, WRP said: I understand the argument of using Local/Global for code readability, but I don't see it for avoiding collisions. As far as I can tell, except for the case of Global within a function, AutoIt assigns the same scope to variables whether or not Local/Global is used. So, Local should have absolutely no effect wherever it is used. Is it hard for you to imagine something like this? $i = 5 SomeFunction() MsgBox(4096, "Aha", "$i = " & $i) Func SomeFunction() $i = 4096 $y = 2 MsgBox($i, "...", "wait a bit", $y) EndFunc Try adding "Local" for variables inside the function to see if there'd be any changes. Anyway, variables used only inside a function should be declared as local, not because of readability, but because of correctness. czardas already explained. Edited April 3, 2016 by trancexx ♡♡♡ . eMyvnE Link to comment Share on other sites More sharing options...
WRP Posted April 3, 2016 Author Share Posted April 3, 2016 OK, so the explanation I needed is that without Local/Global, the default behavior is to make a variable declared within a function local UNLESS the name refers to an existing global variable. So the use of Local prevents reference to an existing global variable. Link to comment Share on other sites More sharing options...
BrewManNH Posted April 3, 2016 Share Posted April 3, 2016 That is correct, and is explained in the help file. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator Link to comment Share on other sites More sharing options...
philkryder Posted April 3, 2016 Share Posted April 3, 2016 1 hour ago, WRP said: OK, so the explanation I needed is that without Local/Global, the default behavior is to make a variable declared within a function local UNLESS the name refers to an existing global variable. So the use of Local prevents reference to an existing global variable. If one uses the best practice of having a "g" in the name of global variables, then, collisions would not happen. Link to comment Share on other sites More sharing options...
philkryder Posted April 3, 2016 Share Posted April 3, 2016 14 hours ago, czardas said: Take a look here: https://www.autoitscript.com/wiki/Best_coding_practices#Scopes_of_Variables thanks - Is there any interest in including a recommendation to use the "p" for parameters? They are different from local variables in the that they can be changed and cause a side effect change back in the calling function or mainline code. Link to comment Share on other sites More sharing options...
czardas Posted April 4, 2016 Share Posted April 4, 2016 (edited) 2 hours ago, philkryder said: thanks - Is there any interest in including a recommendation to use the "p" for parameters? Parameters are treated as local variables unless you use the ByRef keyword, so function parameters do not collide with global variables. Remove ByRef from the code below and the global variable will remain zero. Also, scroll up the best coding practices page and you will see that the letter p is already used to indicate a pointer. Global $g_iParam = 0 Go($g_iParam) MsgBox(0, "", $g_iParam) Func Go(ByRef $g_iParam) ; Remove ByRef to test the difference. $g_iParam = 1 EndFunc Edited April 4, 2016 by czardas operator64 ArrayWorkshop Link to comment Share on other sites More sharing options...
guinness Posted April 4, 2016 Share Posted April 4, 2016 16 hours ago, philkryder said: Is there any interest in including a recommendation to use the "p" for parameters? No, as p already has a meaning UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now