MIT 6.00 Intro Computer Science (OCW) OpenStudy (anonymous):

Iterative and recursive seem to be the same. Can anyone explain to me, in layman's terms, the difference? Thx. OpenStudy (anonymous):

A recursive function is a function that calls itself with smaller versions of the original problem until it's in the base case. An iterative program does not call itself. OpenStudy (anonymous):

I tried the plain English way, but this might work better, at the risk of being more confusing. Honestly, the way I think about it is that whenever I see a function calling itself, I imagine a trapdoor. So in the line: else: return fib(x-1) + fib(x-2) I read as far as else: return fib(x-1) and then I think, OK, the current function stops, and (x-1) falls through to fib(x). Until that's completed, the rest of this function is on hold. That could happen hundreds of times, but as long as x isn't in the base case, it's going to keep falling through that trapdoor into smaller and smaller problems. It might help to use the fibonacci example in the lecture, but expand it to show the flow of control. def fib(x): """Return fibonacci of x, where x is a non-negative int""" if x == 0 or x == 1: return 1 else: return fib(x-1) + fib(x-2) So say we call it with 3 as an argument: 1. if x==0 or x==1: return 1 ##Nope 2. else: return fib(x-1) +fib(x-2) a) call fib with 2 as an argument. This is fib(x-1) in line 2. b) If x ==0 or x ==1: return 1 ##nope c) else: return fib(x-1) + fib(x-2) i) call fib with 1 as an argument.This is fib(x-1) in line c. ii) If x ==0 or x ==1: return 1 ##Yes! we can do this! the fib(x-1) in line c = 1 iii) call fib with 0 as an argument. This is fib(x-2) in line c. iv) If x ==0 or x ==1: return 1 ##Yes! we can do this! the fib(x-2) in line c = 1 c) is now completed. Returns 2 as fib(x-1) in line 2 d) call fib with 1 as an argument. This is fib(x-2) in line 2. i) If x ==0 or x ==1: return 1 ##Yes! we can do this! the fib(x-2) in line 2 == 1 2. is now completed. fib(x-1) = 2, fib (x-2) = 1, so line 2 returns 3. OpenStudy (anonymous):

Thanks a million! I think I got it - for example - and feel free to correct this: def recur(x): if x == 0: print "This is finally the end of the recursive loop with value x:", x else: print x, "Where x = x - 1:", (x-1) return recur(x-1) I think this is a good example for beginners to understand what happens...and your layman's term explained it nicely. For anything else more complicated than this...I may be at a loss! ;-) OpenStudy (anonymous):

Exactly. This has also been pretty hard for me to get my head around. I've found that with recursive functions it's important for me to keep in mind what they're actually returning and where they're returning it. In the case of fibonacci, that function is getting an integer whenever it says something like fib(x-1). So if I wanted to understand it without getting that snake-eating-its-own-tail confused feeling, I might think of it as: if x == 0 or x == 1: return 1 else: return [INTEGER] + [INTEGER] and worry about the recursive calls later. it helps me separate the number of recursions from the number of answers being returned (at each level, The number of recursions is based on the size of the initial value--fib(21) recurs way more times than fib(2). But no matter how big the initial value is, you're only ever going to get an integer back from any single function call. OpenStudy (anonymous):

Sorry, should be: It helps me separate the number of recursions from the number of answers being returned. OpenStudy (anonymous):

Thanks somnamniac - you've been quite the help. I'll be going over the recursion lectures anyways...they'll make more sense now.

Latest Questions anna918jnu: You are asked to write an equation for the line of best fit for the scatter plot shown at the right without the use of a graphing calculator.
4 minutes ago 1 Reply 0 Medals sealake101: The British colonists were familiar with the ideas of freedom and rights of citizens as expressed in the _____________ and _______________.
11 seconds ago 11 Replies 2 Medals emaniscool35: how I change pfp come here now
8 seconds ago 96 Replies 5 Medals xxcoffee: so i was righting a story for school and this is what i came up with im not even
4 minutes ago 14 Replies 3 Medals bobbii: Which of the following is something you need to think about throughout unit 5? (3
10 minutes ago 5 Replies 1 Medal WendyMorales2007: Why did Spain want Catholic settlers to come to Louisiana? because Spain wanted t
12 minutes ago 4 Replies 0 Medals youdointlikeme: Round the fraction to the nearest whole number. 2/9
12 minutes ago 13 Replies 1 Medal mjx10: a countryu2019s population in 1992 was 72 million
11 minutes ago 10 Replies 1 Medal different: Which of the following is an accurate statement about the Incas? (5 points) The Incas spoke Quechua and did not have a written language.
16 minutes ago 5 Replies 1 Medal emaniscool35: whats the best way to do ur hw
9 minutes ago 63 Replies 6 Medals