1

So I found this code in a Java textbook and it is a piece of recursion code. I don't really understand what happens when nothing is returned ("return;") and it doesn't explain in the textbook either. I tried searching it on the internet, but couldn't find any answers either.

public static void numbers(int n) {
    if (n == 0) {
        throw new IllegalArgumentException();
    }
    if (n == 1) {
        System.out.print(n);
        return;
    }
    numbers(n - 1);
    System.out.print(", " + n);
}

5 Answers 5

1

When n is 1 it prints n (1) and then stops recursing (the return; unwinds the stack-frame and returns to the caller). In this case, that was when n was 2. Then the method body ends (equivalent to a return;) so it happens again (3)...

Sign up to request clarification or add additional context in comments.

Comments

1

Although the code doesn't return anything, it does take action that changes things, in this case printing something to the output.

If n is 1, it just prints 1.

If n is 2, it calls numbers(1). As noted above, that prints 1. Then it returns back to the caller, which is numbers with n==2. The caller starts with the statement after numbers(n-1), and therefore prints , 2. Thus, the output is 1, 2.

If n is 3, it calls numbers(2). As described in the previous paragraph, this outputs 1, 2 using another recursive call. Then it returns to the caller, which is numbers with n==3. The caller starts with the statement after numbers(n-1), and therefore prints , 3. Thus, the output is 1, 2, 3.

So basically, the output of numbers(n) (for n > 1) is just the output of numbers(n-1), with a comma and n after it.

Comments

0

The purpose of that (example) method is to print a number. It is not designed to return anything.

I don't really understand what happens when nothing is returned ("return;") and it doesn't explain in the textbook either.

Well ... what "happens" is that nothing is returned.

But that is OK, because the recursion method is declared as a void method. And indeed, if you did attempt to either return a value, or assign the result of the call, you would get compilation errors. (Try it and see!)

Note that is applies to all methods / method calls in Java, not just recursive ones.


(It most likely does explain this in your text book, but in an earlier section than the one you are looking at right now. Text books are generally written on the assumption that you will read the text from the beginning ...)

Comments

0

"return;" in a void function has the same meaning as "return val;" in a non-void function. So, "return;" in the example means "To here the function ended, and return to the invoke place where the function invoked." You can also think that: in the end of every void function, there is a "return;", but they are always left out for conciseness.

Comments

0

The purpose of this method is to print a sequence of numbers from 1 to n separated by commas and not to return anything to its caller. So here is how it works (try to simulate in your head - I numbered the lines to facilitate my references.):

 1: public static void numbers(int n) {
 2:    if (n == 0) {
 3:        throw new IllegalArgumentException();
 4:    }
 5:    if (n == 1) {
 6:        System.out.print(n);
 7:        return;
 8:    }
 9:    numbers(n - 1);
10:    System.out.print(", " + n);
11: }

If you call it with 1 as parameter (n = 1), the expression in line 5 is true, line 6 prints "1" to the output and the method returns in line 7, to its caller.
If you call it with 2 as parameter, the expression in line 5 fails, the if block is skipped, line 9 calls number method with n - 1 (=1) as parameter, causing "1" to be shown (as seen in the previous case). The CPU then returns to it's caller and resumes executing line 10, which prints ", 2" to the output. The final output is "1, 2".
If you call it with 3 as parameter, line 9 calls numbers method with 2 as parameter, resulting in the output "1, 2" (as seen in the previous case), then prints ", 3" (in line 10), resulting in "1, 2, 3" as output.
And so on...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.