Guest RemyAF Posted January 11, 2004 Share Posted January 11, 2004 Here is a timer script I use... SetEnv, counter, 0 SetEnv, time, 10 Gosub, Timer MsgBox, 0, Finished, Script Complete. Exit ;== Timer == Timer: EnvAdd, counter, 1 Sleep, 1000 IfNotEqual, counter, %time%, Gosub, Timer Return My comp sci. professor always told us that if you run a routine recursively x number of times, it has to back over itself x times to resume. When I ran a little test script with a few MsgBox commands placed here... IfNotEqual, counter, %time%, Gosub, Timer MsgBox, 0, Test, Recursive backup. Return I found that for whatever value of "time" I had one recursive backup message box, in this case, ten backup boxes. Out of curiosity, I made the following change... IfNotEqual, counter, %time%, Goto, Timer ...and when I ran my test, there was only ONE recursive backup message. Here is my question, and hopefullly it makes sense. In AutoIt, does it remember the most recent Gosub command, and the next Return simply jumps back to that, no matter how many other Goto commands there may have been? And is there a way to visibly step through a script so I can see how it does this little bypass? I think I have it straight in my head, and I don't want to make this post any longer than it already is, but I think it's something like... Gosub Timer Goto Timer Goto Timer... etc Return Resume Am I correct? This is a bit confusing, and while a simple, "Yes that's how it works" would suffice, a little more background would be appreciated. Link to comment Share on other sites More sharing options...
Snarg Posted January 11, 2004 Share Posted January 11, 2004 I could be wrong on this one, but I don't think you can call a function from within that function. If you can, it does not seem like good practice. I know a lot of people hate the 'Goto' statement, but this is how I would write that function: SetEnv, counter, 0 SetEnv, time, 10 Gosub, Timer MsgBox, 0, Finished, Script Complete. Exit ;== Timer == Timer: StartAgain: EnvAdd, counter, 1 Sleep, 1000 IfNotEqual, counter, %time%, Goto, StartAgain Return It's ugly, but it should work. A little reading goes a long way. Post count means nothing. Link to comment Share on other sites More sharing options...
Guest RemyAF Posted January 11, 2004 Share Posted January 11, 2004 The code you wrote is EXACTLY what I have. I don't think I was clear in what I was asking, since I was showing that I was changing my code. I started with using: IfNotEqual, counter, %time%, Gosub, StartAgain ...and I found that it had to back over all of the recursive calls to itself. But when I used: IfNotEqual, counter, %time%, Goto, StartAgain ...no matter how many times it called itself with Goto, when it finally counted up to %time%, it Returned right back to the original Gosub statement. Most programming languages don't allow you to do that. The Gosub/Return combination doesn't work if, in the middle of the subroutine (in this case, StartAgain), you call another subroutine. Link to comment Share on other sites More sharing options...
cmallett Posted January 12, 2004 Share Posted January 12, 2004 (edited) I believe AutoIt2 and AutoHotkey are the same in this respect:Every Gosub creates a new stack layer that must eventually be returned from or exited from. Goto's can jump the same label as a gosub, but do not create a new stack layer.Also, recursion and mutual recursion are possible by doing a Gosub to self or having two subroutines that Gosub to each other.I view these traits as a features, not a limitations.However, unlike AutoIt2, AutoHotkey does not produce the error "return without matching gosub", since due to its hotkey nature, Return will do an Exit by design if there is no caller to return to. Edited January 12, 2004 by cmallett Link to comment Share on other sites More sharing options...
Guest RemyAF Posted January 14, 2004 Share Posted January 14, 2004 cmallett... thanks, that's exactly the answer I was looking for; that AutoIt allows Goto statements to exist in the same "stack layer." I also see those features and limits, and obviously using too many Goto's that need to back over each other is only applicable when it involves HUNDREDS of recurses. And thanks for the info about the Return and Exit thing. Link to comment Share on other sites More sharing options...
Recommended Posts