# Undefined variable of function side while loop

**URL:** https://discourse.julialang.org/t/undefined-variable-of-function-side-while-loop/32668
**Category:** General Usage
**Tags:** question
**Created:** [December 24, 2019, 9:48pm UTC](https://discourse.julialang.org/t/undefined-variable-of-function-side-while-loop/32668 "2019-12-24T21:48:51Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![locluong09](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@locluong09](https://discourse.julialang.org/u/locluong09)
#### Post date: [December 24, 2019, 9:48pm UTC](https://discourse.julialang.org/t/undefined-variable-of-function-side-while-loop/32668/1 "2019-12-24T21:48:51Z")

</div>

I have a function inside a while loop like the code below, but get problem ERROR: UndefVarError: Pre\_predict not defined.

```julia
Pre_predict = zeros(Row, Col)
while true
    N,S,W,E,C,Q = construct_Tran_matrix(Pre_init, **Pre_predict** , Gamma, Z_init, Z_predict, Order)
    LHS, RHS = construct_Coefficient_matrix(N,S,W,E,C,Q,Order,cnt)
    Residual_matrix = Residual(LHS, RHS, Pre_predict, Order, cnt)
    J = Jacobian_matrix(Pre_predict, Order, Gamma, Z_predict, cnt)
    Delta_p = J\Residual_matrix
    if maximum(broadcast(abs, Pre_predict-Pre_init)) < 0.01
        break
    end
end

```

BUT when I change to variable X = 5, it works.

```julia
X = 5
while true
    N,S,W,E,C,Q = construct_Tran_matrix(Pre_init, **X** , Gamma, Z_init, Z_predict, Order)
    LHS, RHS = construct_Coefficient_matrix(N,S,W,E,C,Q,Order,cnt)
    Residual_matrix = Residual(LHS, RHS, Pre_predict, Order, cnt)
    J = Jacobian_matrix(Pre_predict, Order, Gamma, Z_predict, cnt)
    Delta_p = J\Residual_matrix
    if maximum(broadcast(abs, Pre_predict-Pre_init)) < 0.01
        break
    end
end

```

Can some one explain the problem with my code?

---

<div class="post-metadata">

### Author: ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)
#### Post date: [December 24, 2019, 10:11pm UTC](https://discourse.julialang.org/t/undefined-variable-of-function-side-while-loop/32668/2 "2019-12-24T22:11:51Z")

</div>

It looks like you need a `let` block. See also [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

```julia
julia> y = 10
10

julia> while y < 100
        y += y
       end
ERROR: UndefVarError: y not defined
Stacktrace:
 [1] top-level scope at .\REPL[8]:2

julia> let y = 10
        while y < 100
         y += y
        end
        return y
       end
160

```

---

<div class="post-metadata">

### Author: ![locluong09](https://avatars.discourse-cdn.com/v4/letter/l/ba8739/32.png) [@locluong09](https://discourse.julialang.org/u/locluong09)
#### Post date: [December 24, 2019, 10:13pm UTC](https://discourse.julialang.org/t/undefined-variable-of-function-side-while-loop/32668/3 "2019-12-24T22:13:29Z")

</div>

Thank you. I got it.

---

<div class="post-metadata">

### Author: ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)
#### Post date: [December 24, 2019, 10:15pm UTC](https://discourse.julialang.org/t/undefined-variable-of-function-side-while-loop/32668/4 "2019-12-24T22:15:42Z")

</div>

Perfect, you’re most welcome.
