PartyPooper Posted December 1, 2005 Share Posted December 1, 2005 I have several variables that are used throughout my script by multiple functions. If I define them at the top of the script outside of any function using the keyword DIM, SciTE doesn't throw any errors. If I define them within a function using the keyword GLOBAL, unless I redefine them in all functions that they are used in, SciTE throws a - WARNING: $variable: possibly used before declaration. error. What's the accepted way of declaring global variables - at the top of the script or multiple times throughout functions that they are used in? Also, will opt("MustDeclareVars", 1) have any effect on how/where variables are declared? Thanks Link to comment Share on other sites More sharing options...
JSThePatriot Posted December 1, 2005 Share Posted December 1, 2005 (edited) In my programming experience. If you want a global variable you declare it in the global space (top of your script) otherwise the variable should be local. Not sure I explained that well. Edit: Using Local and Global is more organized and if used right can help you keep everything easy to read. I am trying to get into the swing of this as using Dim is just I guess an easy way to do it if you dont know whether it should be Global or Local. If you know use it. Trust me it will help in the future. Though in AutoIt I dont believe (though I could be mistaken) it has much of a difference. Let me know, JS Edited December 1, 2005 by JSThePatriot AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
Valuater Posted December 1, 2005 Share Posted December 1, 2005 i like using ... DIM because i am able continue scripting and there are no errors opt("MustDeclareVars", 1) clearly "you must declare all variables"...so if you are gojng to write a large script that uses alot of memory etc, you should use that option otherwise, use Dim and create what you want 8) hobbiest approach 8) Link to comment Share on other sites More sharing options...
PartyPooper Posted December 1, 2005 Author Share Posted December 1, 2005 Yeah, thanks guys. I've been using DIM to declare all variable at the top of my script, however, as my script is now becoming rather large, I am wanting to start declaring local variables inside functions so they are destroyed on return and my script becomes less memory intensive. What's the difference between declaring a variable GLOBAL (as opposed to using DIM) outside of functions, do you know? Is there any benefit other than it give a visual indication that the variable is global in nature? Cheers Link to comment Share on other sites More sharing options...
JSThePatriot Posted December 1, 2005 Share Posted December 1, 2005 ScopeA variable's scope is controlled by when and how you declare the variable. If you declare a variable at the start of your script and outside any functions it exists in the Global scope and can be read or changed from anywhere in the script.If you declare a variable inside a function it is in Local scope and can only be used within that same function. Variables created inside functions are automatically destroyed when the function ends.By default when variables are declared using Dim or assigned in a function they have Local scope unless there is a global variable of the same name (in which case the global variable is reused). This can be altered by using the Local and Global keywords to declare variables and force the scope you want.I pulled that from the helpfile. Scope is the term I was racking my brain for.Dim'ng inside a function is Local. Dim'ng outside is Global. Basically that is what the above explains. It also explains the exceptions.I hope the above helps answer your last question.JS AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
PartyPooper Posted December 1, 2005 Author Share Posted December 1, 2005 Yeah, I read that prior to posting thank JS. It didn't really explain everything I was after but it did help me to decide to use local variables inside functions to cut down on memory use. One of the things that confused me was, according to how I read that paragraph, if I declared a variable as GLOBAL inside a function, this should have the same effect as if I declared the variable outside the function using DIM. This doesn't appear to be the case, hence, my post. Link to comment Share on other sites More sharing options...
JSThePatriot Posted December 1, 2005 Share Posted December 1, 2005 Yeah, I read that prior to posting thank JS. It didn't really explain everything I was after but it did help me to decide to use local variables inside functions to cut down on memory use.One of the things that confused me was, according to how I read that paragraph, if I declared a variable as GLOBAL inside a function, this should have the same effect as if I declared the variable outside the function using DIM. This doesn't appear to be the case, hence, my post.It should be the case, but SciTE is trying to keep you in "good" programming practices. I believe the script will work if you run it outside of SciTE. I would have to test that theory, but I dont have AutoIt on this computer. I will test when I get home if you dont.I hope that helps more. I figured you had read the section from your post it seemed that way. I posted it more as a reference. I have a tendancy to lose my train of thought as I am trying to type everything I am thinking in.JS Relive 1 AutoIt Links File-String Hash Plugin Updated! 04-02-2008 Plugins have been discontinued. I just found out. ComputerGetInfo UDF's Updated! 11-23-2006 External Links Vortex Revolutions Engineer / Inventor (Web, Desktop, and Mobile Applications, Hardware Gizmos, Consulting, and more) Link to comment Share on other sites More sharing options...
PartyPooper Posted December 1, 2005 Author Share Posted December 1, 2005 Thanks JS, I know how you feel. I stare at code and just go blank sometimes. I know it's time to get up and get a caffeine fix then :-) I'm sure its SciTE as well. I don't mind and it's probably a good thing but it tends to confuse newbies like me when we read the help file and expect things to happen but they don't. I have run the script outside of SciTE and it appears to work without a problem, however, in keeping with good programming practices, I'll modify the way I was going to do things so that SciTE likes it as well. Cheers Link to comment Share on other sites More sharing options...
PartyPooper Posted December 1, 2005 Author Share Posted December 1, 2005 (edited) BTW, if I do declare a local variable inside a function, do I really need to use the keyword LOCAL, or can I leave it out because AutoIt automatically assigns it local scope? EDIT: disregard - SciTE kicks up a fuss if I don't anyhow. Edited December 1, 2005 by PartyPooper Link to comment Share on other sites More sharing options...
MHz Posted December 1, 2005 Share Posted December 1, 2005 Upon using MustDeclareVars, then you must declare first. With MustDeclareVars off: You cannot declare any variable without using Global, Dim or Local, so the answer is No. You can however Assign without declaring. Variables assigned within a function behave as if declared with Dim. So variaibles within functions should be declared Local unless you want a Dim effect to happen. Any variables used without declaring and without assigning first will stop the script with an AutoIt error. Scite does not create a fuss as it is Au3Check that gives warning through Scite's output. Au3Check or AutoIt can separately returns errors to Scites output, yet alone other errors that come from CompileAu3... Where the errors come from can be important to fixing the errors. I hope I have complied with the helpfiles teachings and given a another view to help understanding Link to comment Share on other sites More sharing options...
PartyPooper Posted December 1, 2005 Author Share Posted December 1, 2005 Thanks MHz. I'll read up some more on Assign but when I first looked at it, I presumed it was the equivalent as using Global or Local with an equals sign, eg: Assign ( $var1 , 10, 1 ) Assign ( $var2 , 10, 2 ) same as Local $var1 = 10 Global $var2 = 10 Cheers Link to comment Share on other sites More sharing options...
MHz Posted December 1, 2005 Share Posted December 1, 2005 (edited) I was not refering previously to the Assign() function, but that is another method. To assign a variable without using MustDeclareVars,1 is as such: $var1 = 10 No declaration needed before use. (within functions, acts like Dimming the variable) To declare: Global $var1 The variable has not been assigned a value yet, but it has been declared for use. You can declare and assign in one line: Global $var1 = 10 Edited December 1, 2005 by MHz Link to comment Share on other sites More sharing options...
ChrisL Posted December 1, 2005 Share Posted December 1, 2005 I had one of my scripts that ran and ran in a loop calling functions However, I had a variable in the main body ($Var1) of the script, but I called a function and passed a variable of the same name. Whatever($var1) Func Whatever($var1) stuff $Var1 etc, etc. Endfunc The result was a memory leak. What I should have done was give it a different name in the function, which I did figure out in the end and this fixed my problem. Whatever($var1) Func Whatever($DiffName) stuff $DiffName etc, etc. Endfunc Maybe everyone knows this already but I just thought I should share, hope that makes sense [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire 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