Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 22 Online
OpenStudy (anonymous):

For the 2011 6.00SC. Class I'm on the recursion lecture. I'm trying to figure out with recursion if there is a way to do something (like print) only at the end of all the passes through. For example with the Fibonacci code ( http://pastebin.com/Wt4DKwUf ) at the very end WITHIN the function say "hey the grand total was blah". This would be instead of outside the function or printing "the grand total was blah" 8 times for each pass through for fib(10). On the 'scrabble' problem set I tried to use recursion and got burned on this idea ("the total hand score is...")

OpenStudy (anonymous):

Actually think I answered my own question. I can nest a recursive function inside a simple outer one. The outer one will just say "the total is" and call the inner recursive one which returns the total. Derp....any other ways let please let me know!

OpenStudy (anonymous):

In case anyone ever cares this is what I was talking about - my recursive scrabble game solution for ("play_hand") http://pastebin.com/5yXhPvBn

OpenStudy (turingtest):

The only solution I can think of, at least in the case of the fib function, that would insert the print statement into the recursive function itself is by utilizing dynamic programming, which I think is a bit down the road for you, and an overly-complicated solution for your problem.

OpenStudy (turingtest):

Your idea of a simpler outer function seems the way to go in my opinion.

OpenStudy (rsmith6559):

The "simple outer" function is referred to as a wrapper function. It sets up the recursive function, taking in the args that the recursion will be working on and arranging and maybe supplementing them for the recursive function.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!