If I pass return 0 in the if statement then the factorial return accepts 0 and multiples by 0.
Can someone explain how it works? how is return from if statement is accepted by the factorial return? Is this the same behavior with String return type (in a different type of code)? I'm thinking of testing this with string.
I think your base case should be 0 and the factorial of 0 is 1.
A return statement abruptly completes the whole of the method; it transfers control back to wherever it was called from. No code after the return statement is therefore executed.
Well passing return 2 in if statement causes the value of last factorial return wrongly.
like instead of returning 24 it returns 48 for factorial(4). Something is happening when the "if return is passing other than 1". That's what I'm trying to understand. I'm not sure how the final value is changed or what's happening.
So when you get to 0! you leave behind the result 1; when you get to 1! yoiu multiply that by 1 and leave the result behind, when you get to 2! you multiply that by 2 and leave the result behind, when etc., etc., when you get to 13! you get an arithmetic overflow error. You could get to about 21! if you used longs.
If you get to 1! or 0! and return 2, you are returning the wrong result, so it's hardly surprising you are getting the other calculations wrong.
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad: