   Fibonacci Algorithm + Recursion...don't understand one part
  Well I'm reading this book and it gives an example of recursion using Fibonaccis series. Fibonaccis series is this....2, 3, 5, 8, 13....A particular number is just the sum of the other two before it. Now I've broken down the code in the book and I understand all the parts to it except one part in the fib() function definition...// Listing 5.10 - demonstrates recursion // Fibonacci find. // Finds the nth Fibonacci number // Uses this algorithm: Fib(n) = fib(n-1) + fib(n-2) // Stop conditions: n = 2 || n = 1 #include <iostream.h> #include <conio.h> int fib(int n); int main() { int n, answer; cout << "Enter number to find: "; cin >> n; cout << "\n\n"; answer = fib(n); cout << answer << " is the " << n << "th Fibonacci number\n"; getch(); return 0; } int fib (int n) { cout << "Processing fib(" << n << ")... "; if (n < 3 ) { cout << "Return 1!\n"; return (1); } else { cout << "Call fib(" << n-2 << ") and fib(" << n-1 <<").\n"; return( fib(n-2) + fib(n-1)); } }I don't understand the part where it returns (fib(n-2) + fib(n-1)). For example, how does it know to take n-2 and check that number as opposed to taking n-2 and n-1 and just returning the sum of the two?   
Quote:Original post by OneThreeThreeSevenI don't understand the part where it returns (fib(n-2) + fib(n-1)). For example, how does it know to take n-2 and check that number as opposed to taking n-2 and n-1 and just returning the sum of the two?I don't understand your question.  It runs the fib function with n-1 as input, then with n-2 as input, and returns their sum.  Each of those two calls will in turn call fib on their n-1 and n-2 (which is why this method of computing Fibonacci numbers explodes in time complexity).Can you state your question more clearly?   ;  Thanks, you answered my question.  :p   ;  Just for the sake of contributing: your book is old, and misses a few key points of style:1) old: 'iostream.h' doesn't exist any more. It's just 'iostream' now, and it places its contents into namespace std.2) style: don't artificially pause your programs at the end. Learn to run them properly, instead (e.g. from a command line or by placing a breakpoint at the end).3) style: it's useless to make a variable that's assigned to once and then read from once, as you can just use the expression in place of the variable. The 'answer' variable here is not really making things any clearer, and also violates the principle of declaring things near first use (since in C++, we need not declare things at the top of their scope).4) style: 'return' is a keyword, not a function, so there's no reason to put the thing to return in parentheses.#include <iostream>using std::cout;using std::cin;int fib(int n) {  cout << "Processing fib(" << n << ")... ";  if (n < 3) {    cout << "Return 1!\n";    return 1;  } else {    cout << "Call fib(" << n-2 << ") and fib(" << n-1 << ").\n";    return fib(n-2) + fib(n-1);  }}int main() {  cout << "Enter number to find: ";  int n;  cin >> n;  cout << "\n\n" << fib(n) << " is the " << n << "th Fibonacci number\n";};  Bored hehe. If you have multiple "if else" cases that all return something, you can exclude the "else".int fib(int n) {  if (n < 3) return 1;  return fib(n-2) + fib(n-1);}Now doesn't that look simpler? =)   ;  Oh man...my book is old.  =(  And I was learning so much from it. I was just getting a solid understanding of recursions. Just made a program that adds 100 to numbers entered by the user a certain number of times (also entered by the user. But the techniques in this book are old eh? Can anyone recommend any up to date books for me. I already have "C++ Primer Plus (5th Edition) By Stephen Prada" on the list. I hear thats good. But does anyone have more suggestions for books?   ;  I wouldn't worry about old books too much. I started learning about programming from The Unix Programming Environment which was published in about 1980 or something silly :).   ;Quote:Original post by CromulentI wouldn't worry about old books too much. I started learning about programming from The Unix Programming Environment which was published in about 1980 or something silly :).Really?...so this stuff isn't obsolete or anything like that? And I'm not trying to program for games. I'm just learning this stuff so I can eventually learn asm and reverse engineering. Then that will lead to other stuff.  O_o;  For C++, there actually is some obsolete stuff. For instance, nowadays you'd include <iostream> instead of <iostream.h>. Most of these things won't interfere with learning the fundamentals of programming, though.   ;  Your book should be fine for learning concepts. iostream isn't exactly fundamental for learning recursion.   