Avoiding recursion
BlitzMax Forums/BlitzMax Programming/Avoiding recursion
| ||
Hi all! Something bothering me for awhile. I had been getting weird tricky errors, like an array was only looking through maybe the first few entries, etc., over and over, or something only partially happening, then I realized that my function was being called too quickly over and over - before it was finished with its job. To fix it, I've been doing something like this: ' main game loop While whatever is not true ' do this ' do that ' draw something ' when all ready, move screen: If mdscroll = 0 Then scrollit(x,y) End If Flip Wend Function SCROLLIT:Int(a,b) If mdscroll = 1 Then Return End If mdscroll = 1 ' perform some action, maybe look thru ' a list or whatever. If done, exit For j blah blah If this or that ' explode kingon ship or whatever ' func did what needed. return, reset flag mdscroll = 0 Return End If Next ' just exit if no klingon. reset recursion flag mdscroll = 0 End Function So basically, I'm using a little global flag to make sure the function isn't re-entered by the fast-running loop before said function is done with whatever it needs to do. In fact, this works fine. But somehow doesn't seem elegant or efficient. It seems I need a global flag for all my functions, unless its something like a = a + 1. Is there some method I don't know of that keeps a function from getting re-called before its done? Or a special way to call it? Any tips on better programming methodology in regards to recursion problems would be appreciated. Thanks again! -Robert |
| ||
I believe you misunderstand recursion. Recursion is the process by which a function calls itself to get a job done and there is definitely no function calling itself there. The way you are working out your problem is fine as long as it's giving you the results you want. you could instead of using a global, pass a local variable by reference but it will be the same process. FYI a recursive function: [monkeycode] Function countToTen:Int(c:Int = 0) c :+ 1 Print c If c < 10 countToTen(c) EndIf End Function countToTen() [/monkeycode] in case you didn't know. :) Last edited 2012 Last edited 2013 |
| ||
...? OK, two things here: 1) There is no recursion of any sort in the above example or in your problem description. I don't think recursion is the word you're looking for. For the record, recursion describes the situation where a function calls itself, or calls another function that will in turn call it again; it only refers to this situation. (EDIT: Ninja'd with an example.) 2) A loop is effectively paused while the functions within the loop body are running. Unless you've been playing with multithreading, the situation you describe - "my function was being called too quickly over and over - before it was finished with its job" - is literally impossible. Called by what? There's only one instruction pointer and it's currently within the function, not within the loop. The code posted above will definitely not suffer from the problem you've described and the state variable is completely unnecessary. Perhaps you can post a runnable example that does suffer from the problem? Last edited 2012 |
| ||
recursion is a blessed thing. i use it all the time in fact, my life is just a big recursion Last edited 2012 |