aaronvan
aaronvan

Reputation: 316

"Missing Return Statement" - will not compile

The following method should return the powers of 2 from 0 to n. However, I'm getting an error that says my code did not compile because of missing return statement. Note: this is from an online site associated with my textbook (Building Java Programs, 3E, Reges & Stepp). We are only supposed to just write the method(s) described in the problem statement.

public static int printPowersof2(int n) {
    for (int i = 0; i <= n; i++) {
        int number = (int) Math.pow(2, i);
        return number; 
    }
}

Upvotes: 0

Views: 364

Answers (5)

aaronvan
aaronvan

Reputation: 316

OK, thanks for the help. As noted above, I was confused by void and return. Here is the working method:

public static void printPowersOf2(int n) {
    for (int i = 0; i <= n; i++) {
        int number = (int) Math.pow(2, i);
        System.out.print(number);
        System.out.print(" ");
    }
}

Upvotes: 0

ajb
ajb

Reputation: 31699

You don't actually want to return anything. The question says "Write a method called printPowersOf2 that accepts a maximum number as an argument and prints each power of 2 from 20 (1) up to that maximum power, inclusive." This says nothing about returning any values.

return, and result types, are something you use if you want to be able to call a method in an expression, and use its value. For example:

double d = Math.sqrt(5);

will set d to the square root of 5. For this to work, the sqrt method must be declared to return a value (actually a double), and there has to be a return statement somewhere in the method body of sqrt, which tells it what the value will be. In the above statement, that value, the one that gets return'ed, will be assigned into d.

In your case, though, you don't want to set anything to a value. You will never want to do something like

int something = printPowersOf2(10);

because the purpose of printPowersOf2 is to print results, not to compute a value that can be used in an expression.

Therefore, the result type of the method should be void:

public static void printPowersOf2(int n) {
    ...
}

and the body must not contain any return statements that contain a value. (return; statements with no value are OK.) The body will print values (using System.out.println, probably), but it will not return any values.

Upvotes: 1

user3145373 ツ
user3145373 ツ

Reputation: 8156

Put return outside the loop.

public static int printPowersof2(int n) {
    int sum = 0;
    for (int i = 0; i <= n; i++) {
        int number = (int) Math.pow(2, i);
        sum = sum + number;
        System.out.println(number + " : " + sum); // will print current calculated result & sum of it
    }
   return sum; // I have returned sum, you can return whatever you want
}

Upvotes: 0

Eran
Eran

Reputation: 394086

You must have a return statement outside the loop, since the loop may not be executed at all if n is negative, in which case the method won't have a return value.

public static int printPowersof2(int n) {
    for (int i = 0; i <= n; i++) {
        int number = (int) Math.pow(2, i);
        return number; 
    }
    return 0; // it's up to you to decide what to return in this case
}

That said, as mentioned in the comments, there's a problem with the method's logic. If, as the name suggests, the method should print all powers of 2 up to n, it should not return when i is 0. The return statement should appear only outside the loop.

public static int printPowersof2(int n) {
    int number = 0;
    for (int i = 0; i <= n; i++) {
        number = (int) Math.pow(2, i);
        System.out.println(number);            
    }
    return number;
}

I wasn't sure what the return value should be. If you want to return all the powers of 2 instead of just printing them, your method should return a List or an array.

public static int[] printPowersof2(int n) {
    if (n<0)
        return new int[0];
    int[] result = new int[n+1];
    for (int i = 0; i <= n; i++) {
        result[i] = (int) Math.pow(2, i);           
    }
    return result;
}

Upvotes: 3

Mureinik
Mureinik

Reputation: 312136

If you want to return multiple results (in your case, the powers of 2 from 0 to n), you need to return a collection of some sort, e.g. a list:

public static List<Integer> printPowersof2(int n) {
    List<Integer> list = new ArrayList<>(n);
    for (int i = 0; i <= n; i++) {
        list.add((int) Math.pow(2, i));
    }
    return list;
}

or an array:

public static int[] printPowersof2(int n) {
    int[] result = new int[n];
    for (int i = 0; i <= n; i++) {
        result[i] = (int) Math.pow(2, i);
    }
    return result;
}

Upvotes: 1

Related Questions