The full code for anyone who wants to play around with it:
int factorial(int n)
{
int fallback = 1;
/* This breaks if you remove the comment,
* so leave it in.
*/
if (n == 0) { /* Handle 0! specially.
n = 1; * 0! is the same as 1!.
return factorial(n); */
} else {
return n * factorial(n - 1);
}
return fallback;
}
Then it's a syntax error, but I get what you mean.
intfactorial(intn){int fallback =1;/* This breaks if you remove the comment,
* so leave it in.
*/if(n ==0){Handle0! specially.
n =1;*0! is the same as 1!.returnfactorial(n);}else{return n *factorial(n -1);}return fallback;}
vs (what you probably mean)
intfactorial(intn){int fallback =1;/* This breaks if you remove the comment,
* so leave it in.
*/if(n ==0){
n =1;returnfactorial(n);}else{return n *factorial(n -1);}return fallback;}
vs what the rest was thinking of
intfactorial(intn){int fallback =1;/* This breaks if you remove the comment,
* so leave it in.
*/if(n ==0){}else{return n *factorial(n -1);}return fallback;}
int factorial(int n) {
int fallback = 1; /* This breaks if you remove the comment,
* so leave it in.
*/
if (n == 0) {
} else {
return n * factorial(n - 1);
}
return fallback; }
The comment behind the if removes the whole if-statement, if it is uncommented what I described above happens.
Without the comment:
int factorial(int n) {
int fallback = 1; /* This breaks if you remove the comment,
* so leave it in.
*/
if (n == 0) {
n = 1;
return factorial(n);
} else {
return n * factorial(n - 1);
}
return fallback; }
I didn't know it will compile without a statement following if....
The comment behind the if removes the whole if-statement
It only removes the block for the if/true path - not the whole if/else construct. Your code matches, your words not. Sorry to be nitpicky here, but that's important as it is very easy to cause misunderstandings.
Assuming that int is a fixed-width signed (using 2's complement) integer type with width w, the result is 1 if w=1, 2 if w=2, or 0 if w>2.
Proof: Note that under 2's complement, w-bit integers can be represented using arithmetic modulo 2w, i.e. all congruent numbers have the same 2's complement representation. This both proves that the recursion will eventually reach the base case where n is congruent to 0, and allows us to compute the result of factorial(-1). For w=1, -1 is congruent to 1, so we compute 1! modulo 1, which is 1. For w=2, -1 is congruent to 3, so we compute 3! modulo 4, which is 2. For w>2, we need to compute (2w-1)! modulo 2w. Notice that 2w-2 and 2w-1 are both less than 2w-1, and therefore 2w-2×2w-1=22w-3 divides the factorial. Finally, since w≥3, we have 2w-3≥w, and therefore 2w divides 22w-3, which divides the factorial. Therefore the factorial is congruent to 0 modulo 2w. ∎
So if you attempt to remove the comment without affecting the code, like you do with normal comments, you end up with code that has originally been commented out and thus not been compiled.
The code that has been commented out actually just calls the same function but with 1... and the function with 1 calls the function with 0... and so on and so on.
That said... I think it'd break too if you were to input any number. As the function always calls the function but with the parameter - 1, without any stop.
Ok Seriously. I understand why if you remove the comments it will break. It's an infinite recursion but why does the code, as it is, works fine ?
if we have n == 1 the function will return 1 * factorial(0) and factorial(0) will return nothing because when n == 0 nothing happens. Certainly the recursion stops because we are not calling factorial() again but what I don't understand is what is the value of factorial(0) ? How should we evaluate the return 1 * factorial(0)
The multi-line comment covers the contents of the if (n == 0) case, making it read if (n == 0) {} else { .... As it does nothing if n = 0, it resorts to the fallback of 1.
Kidding aside (I wouldn't downvote for something like that), in that case, I'd recommend adding a meaningful non-/s comment after that... like this comment. It helps to encourage discussion.
92
u/bucket3432 Mar 23 '20
The full code for anyone who wants to play around with it:
Sauce: some entry of {KonoSuba}
Template: Facts you can't destroy at the Animeme Bank
3
u/froggie-style-meme Mar 24 '20
Wait, how does code break if you remove the comment?
2
u/[deleted] Mar 24 '20
Does anybody know the name of the font used for the code section ?
2
u/Dark_Lord9 Mar 24 '20
Ok Seriously. I understand why if you remove the comments it will break. It's an infinite recursion but why does the code, as it is, works fine ?
if we have n == 1 the function will return 1 * factorial(0) and factorial(0) will return nothing because when n == 0 nothing happens. Certainly the recursion stops because we are not calling factorial() again but what I don't understand is what is the value of factorial(0) ? How should we evaluate the return 1 * factorial(0)
53
u/Mr_Piggens Mar 24 '20
The multi-line comment covers the contents of the
if (n == 0)case, making it readif (n == 0) {} else { .... As it does nothing if n = 0, it resorts to the fallback of 1.33
u/froggie-style-meme Mar 24 '20
I’ve been programming almost all of my life yet this is the first piece of code that’s legitimately made me reconsider coding
5
u/MasterQuest Mar 24 '20
Wow, I wonder who thought that having 0 lead to an infinite recursion would be a good idea.
Blessed comment saved the day though.
0
u/[deleted] Mar 24 '20
[deleted] — view deleted comment