-1

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 ints. 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?

asteri
  • 11,402
  • 13
  • 60
  • 84
Deepeshkumar
  • 395
  • 3
  • 13

7 Answers7

3

Every code path through a method with non-void return that returns must return a value.

You have a code path that returns, namely when both ifs evaluate to false, but does not return a value. Namely:

public int gcd(int num1, int num2) {
    if(num1 > num2 && num1 % num2 == 0) {
        return num2;
    }
    if(num1 < num2 && num2 % num1 == 0) {
        return num1;
    }

    // return, but no return value!
}

Note that this does not mean that every possible path must return, just every possible path that does return must return a value. This is legal:

public int M() throws Exception {
    throw new Exception();
}

There are no paths through that method that return that don't return a value.

jason
  • 236,483
  • 35
  • 423
  • 525
3

Does every 'if' should have an else cumpulsory in a function that has a return type other than 'void'?

Not necessarily, so long as every code path returns a value or throws an exception, which is not the case in your method (take, for instance, num1 = 0, num2 = 0; neither if will be entered).

If you know that one of those ifs should be entered based on the values you are passing to the method, you can just add a dummy return at the end of the method (or throw an exception, which would actually probably be better for debugging).

arshajii
  • 127,459
  • 24
  • 238
  • 287
2

You need to have a return statement for all the exection path of your method. You are missing the return statement when both of your if condition fails:

public int gcd(int num1,int num2) {

    if(num1>num2&&num1%num2==0){

       return num2;
    }
    if(num1<num2&&num2%num1==0){

       return num1;
    }

   // add a return statement here as well
}

An advice if you like to follow

Having too many return statemets in the code make it tough for the readers. Setting the value in different if conditions and having a single return is a better choice. Something like:

public int gcd(int num1,int num2) {

    int returnValue = 0;

    if(num1>num2&&num1%num2==0){

       returnValue = num2;
    }
    if(num1<num2&&num2%num1==0){

       returnValue = num1;
    }
    return returnValue;    
}
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
2

plain and simple .. what will happen if both of your if conditions fails , as you have promised compiler that you will return an int , but in the last you are trying to cheat him.

That's why compiler is complaining. :)

Thanks

saurav
  • 3,424
  • 1
  • 22
  • 33
0

You must have a return for all possible paths, including if both of your if statements turn out to be false.

Bill Kowal
  • 19
  • 4
0

You need to return something if num1 == num2 or if num1 and num2 are not divisors (either num1 of num2 or num2 of num1).

Does gcd mean Greatest Common Divisor? If so, then you are probably missing the recursive call:

public int gcd(int num1,int num2) {

  if(num1>num2&&num1%num2==0){
     return num2;
  }
  if(num1<num2&&num2%num1==0){
   return num1;
  }
  return gcd(???, ???);
}
Kristian H
  • 174
  • 5
0

Is every if() required to have an else in a function that has a return type other than void?

I would rather prefer a function whose return type is not void to must return the type. Java compiler expects the same.

Code Enthusiastic
  • 2,827
  • 5
  • 25
  • 39