But does it produce the right answer?
If the compiler has performed "tail recursion optimization" on the code, then you won't see nice recursive calls, only weird jumps and other effects.
But does it produce the right answer?
If the compiler has performed "tail recursion optimization" on the code, then you won't see nice recursive calls, only weird jumps and other effects.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Works perfectly for me, maybe you're doing something weird instead of printing the function's output.
Output:Code:#include <iostream> int recurvsiveFibonacci(int n) { //there is a breakpoint on each of the lines of code if (n == 1 || n == 0) { return n; } else { return (recurvsiveFibonacci(n-1) + recurvsiveFibonacci(n-2)); } } int main() { for (int i = 0; i != 10; ++i) std::cout << recurvsiveFibonacci(i) << ' '; std::cout << std::endl; return 0; }
Code:0 1 1 2 3 5 8 13 21 34
oh well i was under the impression since it jumped around all the time it was messing up. is there a reason it does this? how does that optimize it?
lol i guess i just didnt know those were the right answers. i guess i assumed it was wrong because n was jumping all over the place. oh well would of been nice if the teacher explained hey your compiler might jump all over the place doing recursion functions.
hooch