0

I'm starting learning Java. I have a task to make a recursion function. I was looking for information about recursion in Java and found some interesting code. I can't understand, when n equals 10, why n after "return" n equals 9. And then, when k equals 9, after "return" k = 10.

 public class lvl22666 {

    public static void main(String[] args) {
        recTest(0,10);
    }

    static void recTest(int n, int k) {
        if (n == k) {
            return;
        } else {
            if (n < k) {
                n++;
                System.out.println(n + " " + k);
                recTest (n,k);
            }
            if (k > n) {
                k--;
                System.out.println(n + " " + k);
                recTest (n,k);
            }
        }
    }
}
0

2 Answers 2

4

All recursive methods have three things:

  1. An exit condition, to prevent an endless loop
  2. A work item, and
  3. A recursive call.

The exit condition in your recursive function is:

if (n == k)
   return;

The work item is:

n++;
System.out.println(n + " " + k);

Unless k is greater than n, in which case the work item is:

k--;
System.out.println(n + " " + k);

The recursive call is:

recTest (n,k);

Note that, since a return early-exits you out of the method, the else statement is not required.

To understand the behavior of a recursive method, you must first understand what a stack frame is, how the stack works, and how it serves to preserve state between method calls.

When Java prepares to call a method, it puts the calling methods' local variables including its parameters (collectively, the method's "state") and the return address of the calling method into a stack frame, and pushes that stack frame onto the stack. It then calls the new method.

When Java returns from a method, it pops the stack frame off the stack, restoring the calling method's original state.

A stack is like the stack of plates you see in the carousel at a 50's diner; the first plate off the stack is the last plate the dish washer put there. We call this a last-in, first-out (LIFO) queue.

With a little imagination, you can see how successive calls to a recursive method will keep a running history of any changes made to the state during each recursion. Since a copy of the state is saved during each recursion, you can walk back to a previous step in the state by returning from a method call.

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

Comments

0

What this function does is iterate through it, until n equals k.

if (n == k) {
        return;

This checks if n equals k and returns to the main function, if this is true. If not, it checks which of the two numbers is smaller and adds it by one. After doing that, the function calls itself with the new values of n and k and the whole function repeats. As stated earlier, this is done until n is equal to k which triggers a return. This exits the function and jumps to the next line of code (after recTest(0,10);).

If you have any further questions don't be afraid to ask!

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.