73

I'm looking at a path finding tutorial and I noticed a return statement inside a void method (class PathTest, line 126):

if ((x < 0) || (y < 0) || (x >= map.getWidthInTiles()) || (y >= map.getHeightInTiles())) {
    return;
}

I'm a novice at Java. Can anyone tell me why it's there? As far as I knew, return inside a void method isn't allowed.

Boann
  • 44,932
  • 13
  • 106
  • 138
Relequestual
  • 9,096
  • 6
  • 40
  • 74

7 Answers7

139

It just exits the method at that point. Once return is executed, the rest of the code won't be executed.

eg.

public void test(int n) {
    if (n == 1) {
        return; 
    }
    else if (n == 2) {
        doStuff();
        return;
    }
    doOtherStuff();
}

Note that the compiler is smart enough to tell you some code cannot be reached:

if (n == 3) {
    return;
    youWillGetAnError(); //compiler error here
}
Ian R. O'Brien
  • 6,122
  • 9
  • 40
  • 71
CookieOfFortune
  • 12,980
  • 8
  • 36
  • 56
  • 6
    I understand your code is illustrative, but for the parent's info; I've worked with people that believe each method should only have a single return statement. I'm not one of them, but do believe in minimizing the number of returns as much as possible without making the code ugly in doing it. – digitaljoel Apr 13 '09 at 18:51
  • 3
    Yeah, it's definitely not something to overuse, but sometimes it just makes it a lot easier and can still be very readable. – CookieOfFortune Apr 13 '09 at 19:40
  • 5
    My favorite way of breaking from nested loops :) – b1nary.atr0phy May 13 '13 at 22:10
  • 2
    Without a return value `return` works a lot like `break` in a loop and simply exits the code in question. There have been many flame wars about multiple exit points from functions and a few languages force you one way or the other. I don't take a position on multiple exit points, but I will point out that leaving a function cleans up the local stack. – Michael Shopsin Nov 11 '14 at 15:11
28

You can have return in a void method, you just can't return any value (as in return 5;), that's why they call it a void method. Some people always explicitly end void methods with a return statement, but it's not mandatory. It can be used to leave a function early, though:

void someFunct(int arg)
{
    if (arg == 0)
    {
        //Leave because this is a bad value
        return;
    }
    //Otherwise, do something
}
KBoek
  • 4,659
  • 3
  • 28
  • 43
Pesto
  • 23,272
  • 2
  • 67
  • 76
23

The keyword simply pops a frame from the call stack returning the control to the line following the function call.

MahdeTo
  • 10,296
  • 1
  • 24
  • 27
13

The Java language specification says you can have return with no expression if your method returns void.

John Ellinwood
  • 13,553
  • 7
  • 36
  • 48
3

It exits the function and returns nothing.

Something like return 1; would be incorrect since it returns integer 1.

Albert
  • 518
  • 3
  • 7
  • 16
2

It functions the same as a return for function with a specified parameter, except it returns nothing, as there is nothing to return and control is passed back to the calling method.

Chris Ballance
  • 32,056
  • 25
  • 101
  • 147
1

See this example, you want to add to the list conditionally. Without the word "return", all ifs will be executed and add to the ArrayList!

    Arraylist<String> list =  new ArrayList<>();

    public void addingToTheList() {

    if(isSunday()) {
        list.add("Pray today")
        return;
    }

    if(isMonday()) {
        list.add("Work today"
        return;
    }

    if(isTuesday()) {
        list.add("Tr today")
        return;
    }
}
GreyGoose
  • 570
  • 4
  • 12
iali87
  • 125
  • 8