# unintended recursion

**URL:** <https://community.wolfram.com/t/unintended-recursion/5313>\
**Category:** Questions\
**Tags:** physics, numerical-computation, tuning-and-debugging\
**Created:** [April 2, 2015, 4:00pm UTC](https://community.wolfram.com/t/unintended-recursion/5313 "2015-04-02T16:00:02Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![bruceackerson](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@bruceackerson](https://community.wolfram.com/u/bruceackerson)\
**Post date:** [April 2, 2015, 4:00pm UTC](https://community.wolfram.com/t/unintended-recursion/5313/1 "2015-04-02T16:00:02Z")

</div>

/Users/bruceackerson/Desktop/UpdateVariable.tiff

I understand getting recursion when the Vx variable is on the rhs and lhs. But what do I do, for the recursion in the image?

In case the image doesn’t show

Clear[S1, Vx, [Delta], [CapitalSigma]yyMinusP];

S1 = Vx[i1, j1];

Vx[i1, j1] = S1 + [Delta]/2 + [CapitalSigma]yyMinusP/2;

$RecursionLimit::reclim: Recursion depth of 1024 exceeded. \>\>

---

<div class="post-metadata">

**Author:** ![sblinder](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/sblinder/32/17861_2.png) [@sblinder](https://community.wolfram.com/u/sblinder)\
**Post date:** [April 2, 2015, 4:47pm UTC](https://community.wolfram.com/t/unintended-recursion/5313/2 "2015-04-02T16:47:32Z")

</div>

As you have it written, S1 needs Vx, which, in turn needs S1, etc. Thus an endless loop.  
You might try indexing the variables S[i], Vx[i,j], which would enable finite loops.

---

<div class="post-metadata">

**Author:** ![seanc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/seanc/32/34005_2.png) [@seanc](https://community.wolfram.com/u/seanc)\
**Post date:** [April 2, 2015, 4:51pm UTC](https://community.wolfram.com/t/unintended-recursion/5313/3 "2015-04-02T16:51:32Z")

</div>

The definition of S1 depends on Vx[i1, j1] and the definition of Vx[i1, j1] depends on S.

A simplified example would be:

```
a = b
b = a + 2

```

This is how the evaluator thinks about this.

```
a = b
(* Every instance of the symbol a will now be replaced by the symbol b *)

b = a + 2
(* Oh look! It's the symbol a. Better replace it with b *)

b = b + 2
( * All instances of b should be replaced with b + 2 *)

(* Oh look! it's the symbol b. Better replace it with b+2 *)
b = b + 2 + 2

(* Oh look! it's the symbol b. Better replace it with b+2 *)
b = b + 2 + 2 + 2

(* Oh look! it's the symbol b. Better replace it with b+2 *)
b = b + 2 + 2 + 2 + 2

.....

```

---

<div class="post-metadata">

**Author:** ![bruceackerson](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@bruceackerson](https://community.wolfram.com/u/bruceackerson)\
**Post date:** [April 2, 2015, 5:41pm UTC](https://community.wolfram.com/t/unintended-recursion/5313/4 "2015-04-02T17:41:09Z")

</div>

Has something changed in Mathematica? I looked back at older programs.

S[i] = S[i] + 1

Updated S[i] without recursion. Now it does, even inside a For loop. If I can’t do this without recursion, how does one update a variable to plug back into a function?

---

<div class="post-metadata">

**Author:** ![seanc](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/seanc/32/34005_2.png) [@seanc](https://community.wolfram.com/u/seanc)\
**Post date:** [April 2, 2015, 6:59pm UTC](https://community.wolfram.com/t/unintended-recursion/5313/5 "2015-04-02T18:59:04Z")

</div>

No. This hasn’t changed. It would be a really drastic change to the language.

This is a counterintuitive thing for people people. It’s commonly stumbled over. In other languages that aren’t based on symbolic manipulation, you wouldn’t see this problem, but an error message instead.

If you run (a = a + 1) in a fresh kernel (without definitions) you’ll see that it goes on an infinite loop. The key is that you need to define to a before you can make changes to it. So you should run something like

```
a = 0;
a = a + 1;

```

In most other programming languages, you would get an error message if you tried to increment a without first giving it a value. That doesn’t happen in Mathematica because it allows you to work with addition symbolically.

---

<div class="post-metadata">

**Author:** ![bruceackerson](https://avatars.discourse-cdn.com/v4/letter/b/cab0a1/32.png) [@bruceackerson](https://community.wolfram.com/u/bruceackerson)\
**Post date:** [April 2, 2015, 7:13pm UTC](https://community.wolfram.com/t/unintended-recursion/5313/6 "2015-04-02T19:13:40Z")

</div>

Thanks, I thought I was losing my mind.
