Simple C++ recursion code isn't working how I expected it to
#include <iostream> using namespace std; //a function using recursion to add up numbers in a sequence int add(int a) { if (a < 3) { return a + add(a+1); } else { return 0; // return 0 because otherwise it adds whatever other number to the total } } //how I expected it to run //1 + 2 (a = 1) //1 + 2 + 3(a = 2) //1 + 2 + 3 + 4(a = 3) int main() { cout << add(1); } Assigning 1 as a parameter returns 3, it seems as though the function only runs once where 1 is the parameter(return 1 + 2) Why isn't the function running a second time with 2 as a parameter?(where the total would be return 3(total so far) + 3(current parameter + 1) If I misunderstood something please let me know edit The code returns the desired output if i change the if statement to (a < 4), why?