Help once again with recursion
I shall find the sum of all the even Fibonacci numbers under 4 million. What I don't understand is exactly how Fib(i) relates to the second method (int fib(int f)) I know the code works, but it was I wild guess. I don't know exactly what's going on and I need to comment the code.
The way fib(i) relates to ( int fib(int f) ) is that i is the value passed to the function as the variable f within that function.
As for how recursion itself works...say you called fib(4) (where 4 is the value of i) then this is how recursion would handle that (color coding added to show which function calls correspond to which and which are duplicated, but non-matching function calls):
Thus, the end result of calling Fib(4) would be 5. Without doing your work for you, this is about the best way I can explain how recursion works. Something a drunk college frat boy once told me about programming: When you're confused, draw pictures until it makes sense. I have to give him credit, that advice has worked every time.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now