ChrisL Posted May 27, 2009 Share Posted May 27, 2009 (edited) What is the best practice when calling multiple functions that pass values around to each other. Would you call each function and pass in the parameters? Or set Global Variables and change them? So Func MyFirstFunc($var) ;Do stuff with $Var MySecondFunc($VAR) Endfunc Func MySecondFunc($iVar) Msgbox(0,"",$iVar) EndFunc Or Global $iVar Func MyFirstFunc() ;Do stuff with $iVar MySecondFunc() Endfunc Func MySecondFunc() Msgbox(0,"",$iVar) Endfunc The reason I ask is I have a script which is getting rather complicated passing information around, I was thinking of changing it to use Global values but I wanted to know if it's concidered bad practise Edited May 27, 2009 by ChrisL [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...
dantay9 Posted May 27, 2009 Share Posted May 27, 2009 I like passing on the parameters, but I guess it is a personal preference. Some people like to declare all variables at the beginning and use them throughout their script. Link to comment Share on other sites More sharing options...
Mat Posted May 27, 2009 Share Posted May 27, 2009 Global variables can get messy. fact. MDiesel AutoIt Project Listing Link to comment Share on other sites More sharing options...
martin Posted May 27, 2009 Share Posted May 27, 2009 (edited) I like passing on the parameters, but I guess it is a personal preference. Some people like to declare all variables at the beginning and use them throughout their script. It is a person preference but I think it is far better to avoid global variable because it's safer. For example take your example using a global variable Global $iVar Func MyFirstFunc() ;Do stuff with $iVar MySecondFunc() Endfunc Func MySecondFunc() Msgbox(0,"",$iVar) Endfunc It works fine. Then over a period of time you add code to both functions Global $iVar Func MyFirstFunc() $Firstcal = function3($iVar) ;Do stuff with $iVar MySecondFunc() $secondcalc = function4($iVar);this might not give the expected result because $iVar has been changed by MySecondFunc. Endfunc Func MySecondFunc() Msgbox(0,"",$iVar) $iVar += 13 Endfunc Using Global variables makes maintaining or altering code a lot more difficult IMO, but it might make it easier to write in the first place. But I do find it difficult to avoid using global variables, especially when I use OnEvent mode and functions used for GuiRegisterMessage. Edited May 27, 2009 by martin Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
ChrisL Posted May 28, 2009 Author Share Posted May 28, 2009 Cheers guys martin you almost hit my problem on the head. I have to keep adding to the functions, so what started out as passing 3 parameters is now up to 10 One of the functions is recursive so it passes the 10 paprameters back to the function when it finds a subfolder. I'm going to re-write the functions and I was thinking shall I use Globals or keep passing parameters, after thinking about it I reakon I can simplify it without using Globals and out of the 3 replies that seems favorite. Interesting point about onEvent, fortunately this script doesn't need a gui but I favour onEvent mode for gui's but I've never needed to pass in loads of parameters! (Yet) [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...
Moderators SmOke_N Posted May 28, 2009 Moderators Share Posted May 28, 2009 "Managed" Globals aren't necessarily a bad thing. But I certainly (If could be avoided, and I haven't had a situation yet that it couldn't) would use parameters myself, keep the variables localized, and let them be discarded as need be. Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Juvigy Posted May 28, 2009 Share Posted May 28, 2009 I think the Best practice is using both methods. For example parameters that doesnt change - Globals. For frequently changing pars use parameter passing. Link to comment Share on other sites More sharing options...
ChrisL Posted May 28, 2009 Author Share Posted May 28, 2009 Thanks again [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...
Moderators Melba23 Posted May 28, 2009 Moderators Share Posted May 28, 2009 ChrisL, You mentioned you had up to 10 (expletive deleted!) parameters to pass. I used to use arrays when I had a lot of parameters to pass (only a mere 7 in the worst case) but I have now started using the Dictionary object method used by aGoriila's Hash.au3. Only one parameter to pass and you can use descriptive "keys" for each parameter rather than trying to remember which index number was what. I recommend the UDF wholeheartedly - and as you can see it has a good pedigree! ;-) 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...
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