# Issues with undefined variables in loops?

**URL:** <https://discourse.julialang.org/t/issues-with-undefined-variables-in-loops/24249>\
**Category:** New to Julia\
**Created:** [May 15, 2019, 6:14pm UTC](https://discourse.julialang.org/t/issues-with-undefined-variables-in-loops/24249 "2019-05-15T18:14:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![apattyn](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@apattyn](https://discourse.julialang.org/u/apattyn)\
**Post date:** [May 15, 2019, 6:14pm UTC](https://discourse.julialang.org/t/issues-with-undefined-variables-in-loops/24249/1 "2019-05-15T18:14:42Z")

</div>

I am running a center finite difference method with temporally dependent boundary condition. The following code runs fine.

```julia
dirichlet=1
for Δt in range(2,stop=length(t_array)-1, step=1)
    for Δx in range(1,length=Nx)
        if Δx == 1
            p_series[Δt+1,Δx] = 2*p_series[Δt,Δx]-p_series[Δt-1,Δx]+((δt^2*c0^2)/δx^2)*(p_series[Δt,Δx+1]-2*p_series[Δt,Δx]+dirichlet)
        elseif Δx == Nx
            p_series[Δt+1,Δx] = 2*p_series[Δt,Δx]-p_series[Δt-1,Δx]+((δt^2*c0^2)/δx^2)*(dirichlet-2*p_series[Δt,Δx]+p_series[Δt,Δx-1])
        else
            p_series[Δt+1,Δx] = 2*p_series[Δt,Δx]-p_series[Δt-1,Δx]+((δt^2*c0^2)/δx^2)*(p_series[Δt,Δx+1]-2*p_series[Δt,Δx]+p_series[Δt,Δx-1])
        end
    end
end

```

however when I add an if statement to set the boundary to be 0 after a number of time steps using…

```julia
dirichlet=1
# Start time loop
for Δt in range(2,stop=length(t_array)-1, step=1)
    if Δt == 10
        dirichlet=0
    end
    for Δx in range(1,length=Nx)
        if Δx == 1
            p_series[Δt+1,Δx] = 2*p_series[Δt,Δx]-p_series[Δt-1,Δx]+((δt^2*c0^2)/δx^2)*(p_series[Δt,Δx+1]-2*p_series[Δt,Δx]+dirichlet)
        elseif Δx == Nx
            p_series[Δt+1,Δx] = 2*p_series[Δt,Δx]-p_series[Δt-1,Δx]+((δt^2*c0^2)/δx^2)*(dirichlet-2*p_series[Δt,Δx]+p_series[Δt,Δx-1])
        else
            p_series[Δt+1,Δx] = 2*p_series[Δt,Δx]-p_series[Δt-1,Δx]+((δt^2*c0^2)/δx^2)*(p_series[Δt,Δx+1]-2*p_series[Δt,Δx]+p_series[Δt,Δx-1])
        end
    end
end

```

I get…

```julia
ERROR: UndefVarError: dirichlet not defined
Stacktrace:
 [1] top-level scope at ./REPL[254]:8 [inlined]
 [2] top-level scope at ./none:0

```

I am not quite seeing why this error comes up.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 15, 2019, 6:23pm UTC](https://discourse.julialang.org/t/issues-with-undefined-variables-in-loops/24249/2 "2019-05-15T18:23:22Z")

</div>

You are using global variables in the repl, which now needs an explicit `global` declaration. (There are various posts on discourse about this.)

The solution to this is to wrap everything in a function, which will also significantly improve performance.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [May 15, 2019, 7:08pm UTC](https://discourse.julialang.org/t/issues-with-undefined-variables-in-loops/24249/3 "2019-05-15T19:08:34Z")

</div>

An alternative workflow is to “play around” prototyping things in Jupyter (which will behave the way you are expecting) and then wrap in a function when you want to move it into a `.jl` file.

---

<div class="post-metadata">

**Author:** ![apattyn](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@apattyn](https://discourse.julialang.org/u/apattyn)\
**Post date:** [May 17, 2019, 7:26pm UTC](https://discourse.julialang.org/t/issues-with-undefined-variables-in-loops/24249/4 "2019-05-17T19:26:27Z")

</div>

Thanks! I was mainly just using it to “play around”, so I am not too worried about performance right now.

---

<div class="post-metadata">

**Author:** ![apattyn](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@apattyn](https://discourse.julialang.org/u/apattyn)\
**Post date:** [May 17, 2019, 7:28pm UTC](https://discourse.julialang.org/t/issues-with-undefined-variables-in-loops/24249/5 "2019-05-17T19:28:18Z")

</div>

I may check out Jupyter again, but I was just writing a small script to understand the problem.

---

<div class="post-metadata">

**Author:** ![jlperla](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlperla/32/34332_2.png) [@jlperla](https://discourse.julialang.org/u/jlperla)\
**Post date:** [May 17, 2019, 7:39pm UTC](https://discourse.julialang.org/t/issues-with-undefined-variables-in-loops/24249/6 "2019-05-17T19:39:06Z")

</div>

Most people like to do their “scripts” in jupyter, and then put things into real functions in the `.jl` files with Atom. If you use that approach, you will never run into this issue again.
