# Strange Variable Behavior

**URL:** <https://discourse.julialang.org/t/strange-variable-behavior/9586>\
**Category:** General Usage\
**Created:** [March 8, 2018, 2:50am UTC](https://discourse.julialang.org/t/strange-variable-behavior/9586 "2018-03-08T02:50:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![DiffusionFlame](https://avatars.discourse-cdn.com/v4/letter/d/ee59a6/32.png) [@DiffusionFlame](https://discourse.julialang.org/u/DiffusionFlame)\
**Post date:** [March 8, 2018, 2:50am UTC](https://discourse.julialang.org/t/strange-variable-behavior/9586/1 "2018-03-08T02:50:09Z")

</div>

Hello,

I am new to Julia. I am using Atom and the `uber-juno` package (and its associated packages).

I have discovered some strange behavior. I have a variable to which I’ve saved a time step, `dt=5E-3`. In the workspace it shows the value correctly as `5E-3` but when I go to the REPL and call it, every once in awhile it will return `1E-3`! There doesn’t seem to be a pattern. It just sometimes returns the wrong value for something I thought was saved there.

What could be causing this?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [March 8, 2018, 3:22am UTC](https://discourse.julialang.org/t/strange-variable-behavior/9586/2 "2018-03-08T03:22:24Z")

</div>

Can you share an example?

---

<div class="post-metadata">

**Author:** ![DiffusionFlame](https://avatars.discourse-cdn.com/v4/letter/d/ee59a6/32.png) [@DiffusionFlame](https://discourse.julialang.org/u/DiffusionFlame)\
**Post date:** [March 9, 2018, 12:14am UTC](https://discourse.julialang.org/t/strange-variable-behavior/9586/3 "2018-03-09T00:14:26Z")

</div>

Sure, here is the code snippet. So when I call the variable `dt` from REPL it sometimes returns 1E-3 instead of 5E-3. Any idea why? Any place I could look to further investigate why? Maybe something in the workspace?

```julia
## Input
## =======================================
n = 17; # grid size
U = 1; # velocity [m/s]
H = 1; # square grid height [m]
h = H / ( n - 1 ); # grid spacing [m]
x = linspace(0,H,n)
y = linspace(0,H,n)
dt = 5E-3 # time step [s]

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [March 9, 2018, 12:16am UTC](https://discourse.julialang.org/t/strange-variable-behavior/9586/4 "2018-03-09T00:16:43Z")

</div>

Some other code of yours is modifying `dt`. Search through the code you’ve run and look for `dt`.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 9, 2018, 3:04am UTC](https://discourse.julialang.org/t/strange-variable-behavior/9586/5 "2018-03-09T03:04:54Z")

</div>

> [@mbauman](#):
>
> Some other code of yours is modifying `dt`.

(This is another reason to get out of the habit of writing long scripts that do all computations with local variables. Write functions and pass data with parameters, not globals! Besides being a good programming habit, this is also [crucial for performance in Julia](https://docs.julialang.org/en/stable/manual/performance-tips/#Avoid-global-variables-1).)

---

<div class="post-metadata">

**Author:** ![DiffusionFlame](https://avatars.discourse-cdn.com/v4/letter/d/ee59a6/32.png) [@DiffusionFlame](https://discourse.julialang.org/u/DiffusionFlame)\
**Post date:** [March 9, 2018, 9:25pm UTC](https://discourse.julialang.org/t/strange-variable-behavior/9586/6 "2018-03-09T21:25:06Z")

</div>

Thanks for the replies!

I’ll review my code again but I was getting this behavior in the REPL. I had already run my script and it was done executing. That is why I found it strange and didn’t understand how it could be changing after I had already set it in the script.

Also - thanks for the tip on programming. Let me make sure I understand. You are saying chop up the script and have functions (that i define) do the computations? Call them from a central (shorter) script?
