### recursion

Why does this function count upwards and not downwards? Any help is appreciated thanks.

 ``12345678910111213141516171819`` ``````#include #include void f1(int x); int main() { f1(4); } void f1(int x) { if(x > 0) { int newX = x - 1; f1(newX); printf("%d\n", newX); } } ``````
 ``12345678910111213141516171819202122232425262728293031`` ``````#include #include void f_upwards(int x) { if(x > 0) { int newX = x - 1; f_upwards(newX); std::printf("%d\n", newX); // print after the recursive call } } void f_downwards(int x) { if(x > 0) { int newX = x - 1; std::printf("%d\n", newX); // print before the recursive call f_downwards(newX); } } int main() { std::puts( "upwards: " ) ; f_upwards(4); std::puts( "\ndownwards: " ) ; f_downwards(4); }``````
Why doesn't `void f_upwards(int x)` just loop until 0 then terminate?
 ```f_upwards(4) calls f_upwards(3) f_upwards(3) calls f_upwards(2) f_upwards(2) calls f_upwards(1) f_upwards(1) calls f_upwards(0) f_upwards(0) does nothing and returns to f_upwards(1) f_upwards(1) prints 0 and returns to f_upwards(2) f_upwards(2) prints 1 and returns to f_upwards(3) f_upwards(3) prints 2 and returns to f_upwards(4) f_upwards(4) prints 3 and returns to main()```
Okay thank you!
Topic archived. No new replies allowed.