-1

I have the following:

public static void main(String[] args){
        Screen.clear();
        System.out.println(depth(5,0));
        
    }

public static int depth(int n, int depth){
        System.out.println(depth);
        if(n == 0)return depth;
        else{
           System.out.println(depth);          
           return depth(n-1, depth++);
        }
        
    }

why does this always print out 0, n times? Why isn't depth being incremented?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
DCR
  • 14,737
  • 12
  • 52
  • 115
  • 3
    1. you don't have any pre-increment here. 2. post-increment will *first* return the value *then* increment. And since you only pass in the returned value, it's only the one before it is incremented. So, zero. Same as if you do `int depth = 0; System.out.println(depth++)` you'd still get a zero printed. That's how post-increment always works. Recursion has no real bearing on it. – VLAZ Apr 25 '23 at 15:32
  • 2
    Does this answer your question? [How do the post increment (i++) and pre increment (++i) operators work in Java?](https://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java) – VLAZ Apr 25 '23 at 15:34

2 Answers2

3

you aren't pre-incrementing. Your function is passing 0 before it increments, thereby effectively not incrementing. try this:

public static void main(String[] args){
        Screen.clear();
        System.out.println(depth(5,0));
        
    }

public static int depth(int n, int depth){
        System.out.println(depth);
        if(n == 0)return depth;
        else{
           System.out.println(depth);          
           return depth(n-1, ++depth);
        }
    }

or (if you want to use the post-increment)

public static void main(String[] args){
        Screen.clear();
        System.out.println(depth(5,0));
        
    }

public static int depth(int n, int depth){
        System.out.println(depth);
        if(n == 0)return depth;
        else{
           System.out.println(depth++);          
           return depth(n-1, depth);
        }
    }
DCR
  • 14,737
  • 12
  • 52
  • 115
dbakr
  • 217
  • 1
  • 10
0

The first time you call depth, you pass 5 for n and 0 for depth (btw, in general it's a bad idea to have the same name for methods and parameters). You are doing it like this:

System.out.println(depth(5,0));

Later on, you are calling it like this:

return depth(n-1, depth++);

Let's see what happens:

  • you subtract 1 from n and pass the result to the new function call
  • you pass the unchanged depth and then increment it (depth++ evaluates to its initial value and increments it without, whereas ++depth would increment it first and evaluate the result)

So, these are the values of n and depth respectively upon each call:

  • 5, 0
  • 4, 0
  • 3, 0
  • 2, 0
  • 1, 0
  • 0, 0

To understand this even better, let's try the following:

int i = 1;
System.out.println(i++); //1
System.out.println(i); //2
int j = 1;
System.out.println(++j); //2
System.out.println(j); //2
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175