# Why does the increment/decrement operator cause bugs when used in a recursive function?

**URL:** <https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484>\
**Category:** Beginners\
**Created:** [December 18, 2024, 5:12pm UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484 "2024-12-18T17:12:42Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![WaningJupiter](https://avatars.discourse-cdn.com/v4/letter/w/5e9695/32.png) [@WaningJupiter](https://discourse.processing.org/u/WaningJupiter)\
**Post date:** [December 18, 2024, 5:12pm UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484/1 "2024-12-18T17:12:42Z")

</div>

Hello,  
I have a question about the behavior of the increment/decrement operator in recursive functions.  
Here’s a tree fractal pattern:

```auto
function setup() {
  ...
  angleMode(DEGREES);
}

function draw() {
  ...
  drawBranch(width/2, height,0, 3);
}

function drawBranch(x, y, agl, n){
  let x1 = x - 50*sin(agl); 
  let y1 = y - 50*cos(agl);
  line(x, y, x1, y1); 
  if(n > 1){
    drawBranch(x1, y1, agl-30, n--); 
    drawBranch(x1, y1, agl+30, n--); 
  }
}

```

Instead of a symmetrical tree, using `n--` results in an issue where the branches are not drawn symmetrically, as shown here:

 ![tree](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/a/c/acdef7b2e6b2fe303a6788fb0a8c78888d880c52.png)

I fixed this by explicitly using `n - 1` instead of `n--` in the recursive calls:

```auto
function drawBranch(x, y, agl, n){
 ...
  if(n > 1){
    drawBranch(x1, y1, agl-15, n-1); 
    drawBranch(x1, y1, agl+15, n-1); 
  }
}

```

With this change, the tree is drawn correctly. Could anyone explain why the `n--` caused this issue in the first place?

Thanks !

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [December 18, 2024, 5:45pm UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484/2 "2024-12-18T17:45:01Z")

</div>

Hi @WaningJupiter,

_Sorry, but isn’t that obvious?_ Just joking 🙂

`n--` stands for `n=n-1`

After each operation of `n--`, `n` is changing and you doing it twice in your code. So, for the first call of `drawBranch`, ‘n’ is used and changed and on the second call it uses that changed value (and change further).  
Your solution is the proper way to do in this case! 👍

Hope that helps to clarify…

Cheers  
— mnse

PS: this is not related to recursion, you would have the same in other cases.

EDIT: add JavaScript doc

> **[Decrement (--) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Decrement)**
>
> The decrement (--) operator decrements (subtracts one from) its operand and returns the value before or after the decrement, depending on where the operator is placed.

> **[Increment (++) - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment)**
>
> The increment (++) operator increments (adds one to) its operand and returns the value before or after the increment, depending on where the operator is placed.

> Actually, for java (but good explanation though)  
> [Increment (++) and Decrement (–) Operators in Java Explained](https://codedamn.com/news/java/increment-decrement-operators-in-java-explained)

---

<div class="post-metadata">

**Author:** ![WaningJupiter](https://avatars.discourse-cdn.com/v4/letter/w/5e9695/32.png) [@WaningJupiter](https://discourse.processing.org/u/WaningJupiter)\
**Post date:** [December 18, 2024, 5:56pm UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484/3 "2024-12-18T17:56:57Z")

</div>

Hello, @mnse  
Thank you for the link, 😀 . I hope this will help others who run into the same problem.

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 18, 2024, 7:05pm UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484/4 "2024-12-18T19:05:32Z")

</div>

> [@mnse](#):
>
> `n--` stands for `n = n - 1`.

Actually it is the pre-decrement version `--n` which is equivalent to `n = n - 1` or `n -= 1`.

The post-decrement `n--` has no true equivalent, b/c even though it also decreases the value of `n` by 1, it still evaluates as the original value of `n`.

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [December 18, 2024, 7:33pm UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484/5 "2024-12-18T19:33:12Z")

</div>

Oh… I had always assumed that `n--` and `--n` are the same thing and I often would decide on a whim which variation I’d use. But thinking about it, this is not the type of looseness that a programming language would offer…

Which one of these is the “proper” format? Or do each of them have their respective use-cases?

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [December 18, 2024, 7:42pm UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484/6 "2024-12-18T19:42:01Z")

</div>

`--n` says to decrement first and then use the value. `n--` says to use the value and then decrement afterwards. Consider

```auto
int n = 5;
int x = n--;
println( n, x ); // prints 4, 5

n = 5;
x = --n;
println( n, x ); // prints 4, 4

```

Both are correct and each has its use depending what you need. As stand-alone code, `n--;` is probably more common, but no more proper than the other.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [December 18, 2024, 8:18pm UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484/7 "2024-12-18T20:18:25Z")

</div>

Hi @GoToLoop ,

> [@GoToLoop](#):
>
> Actually it is the pre-decrement version `--n` which is equivalent to `n = n - 1` or `n -= 1`.
> 
> The post-decrement `n--` has no true equivalent, b/c even though it also decreases the value of `n` by 1, it still evaluates as the original value of `n`.

I would only partially agree. 🙂  
The operation is the same in both cases, just the utilisation of `n` differs in the operation.

From my context, I described the sequence of the operation for `n--` in this case…

> [@mnse](#):
>
> , for the first call of `drawBranch`, ‘n’ **is used and changed**

But could had a _afterwards_ in addition to be more precise. 😉

Cheers  
— mnse

---

<div class="post-metadata">

**Author:** ![scudly](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/scudly/32/5597_2.png) [@scudly](https://discourse.processing.org/u/scudly)\
**Post date:** [December 18, 2024, 8:33pm UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484/8 "2024-12-18T20:33:15Z")

</div>

We should also note that these increments and decrements are evaluated as they occur within expressions. For instance, try to figure out what this code will print:

```auto
int x = 5;
int y = x++ - x + --x;
println(x, y);

```

---

<div class="post-metadata">

**Author:** ![eightohnine](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eightohnine/32/19793_2.png) [@eightohnine](https://discourse.processing.org/u/eightohnine)\
**Post date:** [December 19, 2024, 9:56am UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484/9 "2024-12-19T09:56:34Z")

</div>

You know… up until this day, I never gave this two thoughts. And everything worked just fine. But now that I’ve been thinking about your example I feel that everything I know about programming is beginning to unravel.

I’ve always operated with the assumption “The right side of a statement gets evaluated and then assigned to the left side”:

```auto
int a = 1;
int b = 2;
int c = a + b;
println(c); // 3

```

Therefore, in your example I would have bet a lot on x being 4.

```auto
int n = 5;
int x = n--;
println( n, x );

```

To me – a self-taught hobby-coder with no CS background – `n--` is one unit. It makes the value of n “one smaller” and only afterwards that expression evaluation gets assigned to the left side of the statement. The notion that it’s different is somewhat concerning to me. The idea that this `n--` “unit” is split into separate operations is weird.

`n--` by itself reads to me as _“n is one smaller”_  
but `int x = n--;` now reads _“assign n to x and then make n one smaller”_  
This is very unintuitive to me.

The question is: in what conceptual way does the expression `a+b` differ from the expression `n--`? To me this is the same idea…

~_A few hours later_~

Answering my own question:

I’ve read into this a little and learned about _purity_, _state modification_ and _side effects_.

`a+b` is a _pure_ expression, meaning it does not alter the program state. This expression has no side effects.

`n--` is an _impure_ expression, because it affects the state of the program by modifying n. This expression has a side effec, i.e. making n one smallert.

I also understand that this is something that’s more relevant in Functional Programming.  
Still, something I need to adjust my previous assumptions on.

Thanks for the comment and insights!

---

<div class="post-metadata">

**Author:** ![WaningJupiter](https://avatars.discourse-cdn.com/v4/letter/w/5e9695/32.png) [@WaningJupiter](https://discourse.processing.org/u/WaningJupiter)\
**Post date:** [December 19, 2024, 12:26pm UTC](https://discourse.processing.org/t/why-does-the-increment-decrement-operator-cause-bugs-when-used-in-a-recursive-function/45484/10 "2024-12-19T12:26:33Z")

</div>

Hello again,

I accidentally posted a code snippet in p5.js 😅, but the code was originally written in Processing. Thanks for correcting my tag!

I add a follow-up here for Java and JavaScript, I may assume the post increment and decrement operators behave the same way in both Java and JavaScript.

```auto
int n = 5; 
println(n--); // returns 5, n-- is a shorthand of n = n-1, it's an assignement, so n's value will change; 
println(n); // returns 4, n has been assigned a new value by n -1; 

```

```auto
int n = 5; 
println(n-1); // returns 4, n-1 is an expression, not an assignement, this won't change the value of n, 
println(n); // returns 5, n will not change, 

```

In my original code, I called two functions in succession by using `n--`, that’s the reason of the bug :

```auto
if(n > 1){
 drawBranch(x1, y1, agl-15, n--); //At this line, n is assigned then updated to n - 1, its value has changed before being passed into the second function.
 drawBranch(x1, y1, agl+15, n--); 
  }

```

Thanks a lot for the helpful discussion, everyone!
