When using recursive functions, how can you exit the function to return to the parent procedure that called it?
some_function()
if somevar != apples call some_function
If that looped 5 times and then apples were found how would you exit the entire process back to what called some_function to begin with?
You need to specify an exit condition, a branch that proceeds that doesn't recurse.:
def fibo(n):
if n == 0:
return 0 # exit condition
elif n == 1:
return 1 # exit condition
else:
return fibo(n-1) + fibo(n-2)
Very inefficient implementation btw.
shouldn't your function just do that as a consequence of being done?
like, your last iteration returns None, which results in the parent procedure returning None, and so on
Mainly I was reflecting on the old game Trade wars. Tons of fun, but I wanted to try and see how you could make the virtual universe that it ran off of. This involves sectors of pointing to other sectors and so forth. I made a sample to generate a really rough universe. I figured the mapping for finding the fastest path through adjacent sectors to the destination should use a recursive function(It worked in my mind). After testing this im a bit lost to how to do this.
Quote from: Imperceptus on October 22, 2007, 08:25 PM
Mainly I was reflecting on the old game Trade wars. Tons of fun, but I wanted to try and see how you could make the virtual universe that it ran off of. This involves sectors of pointing to other sectors and so forth. I made a sample to generate a really rough universe. I figured the mapping for finding the fastest path through adjacent sectors to the destination should use a recursive function(It worked in my mind). After testing this im a bit lost to how to do this.
I guess you could do that, but it would be really very messy
You should never have to use recursive functions. Ever. It's bad programming practice. Plus, you're pwning stack.
Save that kinda stuff for functional shit like LISP. (heh. yegg is going to have fun posting in this topic.)
Quote from: brew on October 22, 2007, 08:28 PM
You should never have to use recursive functions. Ever. It's bad programming practice. Plus, you're pwning stack.
Save that kinda stuff for functional shit like LISP. (heh. yegg is going to have fun posting in this topic.)
In languages that allow you to create cleaner and more "to the point" code, recursion is a very useful tool. I'm sure banana fanna fo fanna would also agree.
Recursive functions make you cooler, and more dangerous
Chea
Quote from: squiggly on October 22, 2007, 08:51 PM
Recursive functions make you cooler, and more dangerous
Chea
That's the smartest thing I've ever heard anyone say about anything.
I have found recursion really good in projects that involve heirarchy or however thats spelled. Guess I will have to think of a better way to find a path. If anyone has an idea, pls pm me(or mod split this topic with replies). dont wanna have a scrambled topic.
And yes Squiggly, very VERY messy. If you figure every sector has 5 possible adjacent sectors that it can go to to find a possible shortest path.
This is a very common problem when working with graphs. Dijkstra's algorithm (http://en.wikipedia.org/wiki/Dijkstra's_algorithm) will solve this problem for you.
Quote from: K on October 22, 2007, 09:32 PM
This is a very common problem when working with graphs. Dijkstra's algorithm (http://en.wikipedia.org/wiki/Dijkstra's_algorithm) will solve this problem for you.
QFT
T.T