Function design: byref or global? - Ask for Help - AutoHotkey
文章推薦指數: 80 %
Function design: byref or global? - posted in Ask for Help: I am designing a function which changes the values of multiple global variables ... Jumptocontent SignIn CreateAccount Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate AutoHotkey Forums Members Search Advanced Searchsection: Thistopic Forums Members HelpFiles FacebookTwitter AutoHotkeyCommunity AutoHotkey AskforHelp ViewNewContent Functiondesign:byreforglobal? Startedby maxand ,Mar02201311:20AM Pleaselogintoreply 4repliestothistopic maxand Members 166posts Lastactive:Apr15201412:58PM Joined:03Apr2009 Iamdesigningafunctionwhichchangesthevaluesofmultipleglobalvariablesandamunclearwhethertopassvariables(arguments)toitusingbyref,ortodeclarethemasglobalvariableswithinthefunction. Bothapproachescanachievethesameendresult.Thefirstexampleusesbyref: F1:: g_varA:="" SetVarA(g_varA) MsgBox,,,[2]g_varA=|%g_varA%|;result:"1" SetVarA(byrefvarA){ varA:="1" MsgBox,,,[1]g_varB=|%g_varB%|;result:"" } return Thesecondexampledispenseswithbyrefanddeclaresthevariable,withinthefunction,asglobal: F2:: g_varB:="" SetVarB() MsgBox,,,[2]g_varB=|%g_varB%|;result:"1" SetVarB(){ globalg_varB g_varB:="1" MsgBox,,,[1]g_varB=|%g_varB%|;result:"1" return } return Tokeepthingssimple,theseexampleschangeonlyoneglobalvariable;butsinceIintendtochangesetsofmultiplevariablesusingthisfunction,theuseofreturnattheendofthefunctionisinapplicableasitreturnsonlyonevalue.Likewise,passingbyvalueisoutasIneedtochangethevaluesoftheactualvariables.Usingassume-globalwouldsavesomecodehere. Notethatitseemsfromtheabovethatwhenavariableispassedbyreference,thechangesmadetoitbythefunctionarenotapparentuntilthefunctioncompletesrunning,whereasifthevariableisdeclaredasglobal,changesmadetoitwithinthefunctionareimmediatelyevident. UptillnowIhavereservedglobaldeclarationswithinfunctionsforconstantsusedthroughoutthescript.I'dreallyappreciatesomeadviceabouthowtodecidewhentopassbyreferenceandwhentodeclareglobal.Thanksinadvance.UsingAHKv1.0.48.05. #1 - Posted02March2013-11:20AM Backtotop Lexikos Administrators 9844posts AutoHotkeyFoundation Lastactive: Joined:17Oct2006 Ifyouwon'tbereusingthefunctiontomodifydifferentvariables(i.e.thosespecificglobalvariablesaretheonlyvariableswhichyou'llusethefunctionchange),youmayaswellassigntotheglobalvariablesdirectly. whenavariableispassedbyreference,thechangesmadetoitbythefunctionarenotapparentuntilthefunctioncompletesrunning False. F(v) F(ByRefp){ p:=123 ListVars MsgBoxObservethatthefunctionhasn'tcompleted,butthechangetovisalreadyapparent. } InSetVarA,youhavealocalvariablecalledg_varB(i.e.youforgottodeclareitglobal). #2 - Posted02March2013-11:26AM http://ahkscript.org Backtotop maxand Members 166posts Lastactive:Apr15201412:58PM Joined:03Apr2009 ThanksLexikos.Youareright.Worse,Iscrewedupmycodeexample.Itshouldhavereadsomethinglikethis: F1:: A:="" B:="" ;moresets ;C:="";etc. ;D:="" SetVars(A,B) MsgBox,,,[2]A=|%A%|,B=|%B%|;result:A=|1|,B=|1| ;SetVars(C,D);etc. SetVars(byrefX,byrefY){ X:="1" Y:="1" MsgBox,,,[1]A=|%A%|,B=|%B%|;result:A=||,B=||;appliesonlytoset[A,B] } return Lookingbackatthisproblemnow,byrefistheonlywaytogoifImeantopassmorethanonesetofvariablestoSetVars.E.g.,[A,B],[C,D],[E,F],etc.IfIdeclaredthemallglobalwithinthefunction,Iwouldhavetoremembertoincludeallofthemandthiswouldresultinunnecessaryduplicationwithinthefunction,defeatingitspurpose.Mybad. #3 - Posted02March2013-01:33PM Backtotop Lexikos Administrators 9844posts AutoHotkeyFoundation Lastactive: Joined:17Oct2006 Again,youforgottodeclareAandBglobalinsidethefunction. IfyouuseListVarsaboveMsgBoxinsidethefunction,itwillshowyouthatyouhavelocalAandBwhichareblank,andglobalAandBwhicharenot. #4 - Posted03March2013-12:17AM http://ahkscript.org Backtotop Scoox Members 194posts Lastactive:Jun09201703:34AM Joined:28Nov2010 Themainadvantageofglobalvariablesisthattheycanbeusedbyseveralfunctionsandyoudon'tneedtolisttheByRefvariableseachtimeyoucallthefunction,whichcanresultinverylonglinessometimes(thinkofafunctionwith10ByRefparameters).Also,codewhereafunctioniscalledwithinanotherfunctionusuallymeansthattheparametersmustbepassedthroughtheparentfunction,whichcanendupwithevenlongerparameterslists. Forsmallscripts(<500lines)IfindusingglobalsandGoSubisOK,butforlongerscriptsfunctions(vsGoSub)andfunctionparametersandconservativeuseofglobals(vsonlyglobals)makescodesignificantlyeasiertomanageandreducesthelikelihoodofbugs(e.g.imaginetworoutinesmodifyingthesameglobalvariableatthesametime). #5 - Posted26October2014-04:43AM RegJump()-JumptoregistrypathinRegedit HoverScroll()-HoverScroll()-Scrollcontrolswithoutfocus Backtotop BacktoAskforHelp Replytoquotedposts Clear AutoHotkeyCommunity →AutoHotkey →AskforHelp CommunityForumSoftwarebyIP.BoardLicensedto:AutoHotkeyFoundationLLC ChangeTheme IP.Board IP.BoardMobile Ortem English(USA) English(USA) Deutsch PrivacyPolicy Help SignIn UseFacebook UseTwitter Registernow! Usernameoremail: I'veforgottenmypassword ForumPassword Rememberme Thisisnotrecommendedforsharedcomputers Signinanonymously Don'taddmetotheactiveuserslist
延伸文章資訊
- 1returning values from an AutoHotkey function with byref
- 2Function design: byref or global? - Ask for Help - AutoHotkey
Function design: byref or global? - posted in Ask for Help: I am designing a function which chang...
- 3AutoHotkey-Scripts/Functions.ahk at master - GitHub
A collection of my AutoHotkey scripts and libraries - AutoHotkey-Scripts/Functions.ahk at master ...
- 4Return multiple values from AutoHotkey functions with byRef
In my initial video I talked through how & why to use AutoHotkey functions as well as how to retu...
- 5Get String Value of passed ByRef Variable - Stack Overflow
ahk 002: myvar := "george" 003: passit(myvar) 007: MsgBox,GetOriginalVariableNameForSingleArgumen...