I am writing a function to calculate the greatest common denominator of two numbers. The return type of my function is int
and its arguments are two int
s. The code is not complete yet, but so far it has two if()
blocks.
Here is my code:
public int gcd(int num1,int num2) {
if(num1>num2 && num1%num2==0){
return num2;
}
if(num1<num2 && num2%num1==0){
return num1;
}
}
The IDE shows a missing return statement
error. If I declare the function return type as void
and use System.out.println()
statements it works fine. So why does this error occur when the return type is changed to int
, as above?
Is every if()
required to have an else
in a function that has a return type other than void
?