# UndefVarError although I have defined it in the function before using it

**URL:** <https://discourse.julialang.org/t/undefvarerror-although-i-have-defined-it-in-the-function-before-using-it/68087>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [September 13, 2021, 10:59am UTC](https://discourse.julialang.org/t/undefvarerror-although-i-have-defined-it-in-the-function-before-using-it/68087 "2021-09-13T10:59:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sudhanshu\_Mishra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sudhanshu_mishra/32/29093_2.png) [@Sudhanshu\_Mishra](https://discourse.julialang.org/u/Sudhanshu_Mishra)\
**Post date:** [September 13, 2021, 10:59am UTC](https://discourse.julialang.org/t/undefvarerror-although-i-have-defined-it-in-the-function-before-using-it/68087/1 "2021-09-13T10:59:13Z")

</div>

I’m quite new to Julia but i’ve been getting this error for a while. I created a toy scenario similar to this where I define a variable just before using it in a function and it works there. However, I get an UndefVarError while doing the same here.  
I’m working on the following function in ChaosTools.jl.

```julia
function rescale!(integ::MinimalDiscreteIntegrator{true, Vector{S}}, a) where {S<:Vector}
    @. du = integ.u[1] + (integ.u[2] - integ.u[1])/a
    set_state!(integ,du,2)
    # @. integ.u[2] = integ.u[1] + (integ.u[2] - integ.u[1])/a
    # u_modified!(integ, true)
end

```

```julia
Got exception outside of a @test
  UndefVarError: du not defined
  Stacktrace:
    [1] rescale!(integ::DynamicalSystemsBase.MinimalDiscreteIntegrator{true, Vector{Vector{Float64}}, 3, DynamicalSystemsBase.var"#27#28"{DynamicalSystemsBase.DiscreteDynamicalSystem{true, Vector{Float64}, 3, var"#g#8", Nothing, DynamicalSystemsBase.var"#6#12"{ForwardDiff.JacobianConfig{ForwardDiff.Tag{DynamicalSystemsBase.var"#5#11"{var"#g#8", Nothing, Int64}, Float64}, Float64, 3, Tuple{Vector{ForwardDiff.Dual{ForwardDiff.Tag{DynamicalSystemsBase.var"#5#11"{var"#g#8", Nothing, Int64}, Float64}, Float64, 3}}, Vector{ForwardDiff.Dual{ForwardDiff.Tag{DynamicalSystemsBase.var"#5#11"{var"#g#8", Nothing, Int64}, Float64}, Float64, 3}}}}, DynamicalSystemsBase.var"#5#11"{var"#g#8", Nothing, Int64}, Vector{Float64}}, Matrix{Float64}, true}, Int64}, Nothing}, a::Float64)
      @ ChaosTools e:\Master Thesis\ChaosTools\ChaosTools.jl\src\chaosdetection\lyapunovs.jl:339

```

I can run the function by simply doing this

```julia
    set_state!(integ,integ.u[1] + (integ.u[2] - integ.u[1])./a,2)

```

however that makes for messy code and I think that way should be working.  
Thank you.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 13, 2021, 11:13am UTC](https://discourse.julialang.org/t/undefvarerror-although-i-have-defined-it-in-the-function-before-using-it/68087/2 "2021-09-13T11:13:21Z")

</div>

Try `du = @. ...` instead of `@. du = ...`. The reason is that `@. x = y` expands into `x .= y`, which means copy y to existing x.

---

<div class="post-metadata">

**Author:** ![Sudhanshu\_Mishra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sudhanshu_mishra/32/29093_2.png) [@Sudhanshu\_Mishra](https://discourse.julialang.org/u/Sudhanshu_Mishra)\
**Post date:** [September 13, 2021, 11:27am UTC](https://discourse.julialang.org/t/undefvarerror-although-i-have-defined-it-in-the-function-before-using-it/68087/3 "2021-09-13T11:27:28Z")

</div>

Thanks man that worked 🙏. I spent almost a day trying to fix this. 😩

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [September 13, 2021, 11:29am UTC](https://discourse.julialang.org/t/undefvarerror-although-i-have-defined-it-in-the-function-before-using-it/68087/4 "2021-09-13T11:29:36Z")

</div>

Happy to help. When code involving macros in general acts strangely give `@macroexpand` a shot:

```julia
julia> @macroexpand @. du = some(stuff[going].on)
:(du .= some.((stuff[going]).on))

```
