0
If you send fact(0) it will breack?
In the exaple it use: int factorial(int n) { if (n==1) { return 1; } else { return n * factorial(n-1); } } I think that line of code should break and we need to use a conditional in that case.
7 Respostas
+ 1
It will spew out nonsense, yeah. The numbers will go into the negatives until n is so small that it wraps around (integer overflow) at which point it will become huge, and then n will continue decreasing until it is 1.
You should probably add a "if(n<1)" check, just to catch everything :) For a quick example the code is fine though.
+ 1
You should use
if(n<=1)
return 1;
0
you can use:
if(n==0)
return 1;
// this will include '0' as well.
0
so as I suspected the c++ example in the lesson has a bug
0
I guess they are not expecting us to find 0!
0
thats how Bugs happen
0
very true