I have the following function that implements division without the use of a modulus operator
function division($dividend, $divisor, $quotient){
if($dividend<=$divisor){
return $quotient;
}else{
$dividend-=$divisor;
$quotient++;
division($dividend, $divisor, $quotient);
}
}
echo division(3, 2, 0);
I’m confused why I need to prepend the return keyword to the line division($dividend, $divisor, $quotient) if the function is going to iterate until the if statement evaluates to true and return $quotient evaluates.
Can someone explain why the return keyword is needed?
This is because once the quotient has been calculated, you might want to do something with it. In your case you are displaying it with the
echostatement. Without the return call, you cannot know what the result of the function is.