86

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
  • 48,794
  • 16
  • 117
  • 146
Relequestual
  • 11,631
  • 6
  • 47
  • 83

8 Answers8

154

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,682
  • 9
  • 45
  • 73
CookieOfFortune
  • 13,836
  • 8
  • 42
  • 58
  • 7
    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
  • 4
    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
  • 7
    My favorite way of breaking from nested loops :) – arkon 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
31

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
  • 5,794
  • 5
  • 32
  • 49
Pesto
  • 23,810
  • 2
  • 71
  • 76
28

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

MahdeTo
  • 11,034
  • 2
  • 27
  • 28
15

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

John Ellinwood
  • 14,291
  • 7
  • 38
  • 48
5

It exits the function and returns nothing.

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

Albert
  • 536
  • 3
  • 7
  • 16
3

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
  • 600
  • 4
  • 13
iali87
  • 147
  • 10
3

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
  • 33,810
  • 26
  • 104
  • 151
0

void methodName() {

// body }

} ``

} The return statement should not be present within a void method, as a return value cannot be specified.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 19 '23 at 10:19