+ 1

Help...why output is 01234( not 0)?

void recurse(int u) { if(u>0){recurse(u-1);} cout<<u; } int main() { int i=5; recurse(i); }

1st Sep 2018, 1:14 PM
pro
pro - avatar
2 Answers
+ 2
thats because after the function hits its last recursion the value of u is 0,but then it goes back to the recursion where the value of u is 1,goes out of the if braces and continues to the line "cout << u",and so on till u is 2,3 then 4. to fix this,put an else expression after the braces,so it will set the value to 0 only if u is 0.
1st Sep 2018, 1:21 PM
gil gil