# Strange scope issue

**URL:** <https://discourse.julialang.org/t/strange-scope-issue/9759>\
**Category:** New to Julia\
**Created:** [March 16, 2018, 2:28pm UTC](https://discourse.julialang.org/t/strange-scope-issue/9759 "2018-03-16T14:28:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ndbecker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ndbecker/32/10829_2.png) [@ndbecker](https://discourse.julialang.org/u/ndbecker)\
**Post date:** [March 16, 2018, 2:28pm UTC](https://discourse.julialang.org/t/strange-scope-issue/9759/1 "2018-03-16T14:28:29Z")

</div>

In this code, the variable inside the loop “c” is not referenced before it’s value is assigned at runtime, but julia will throw an error  
ERROR: UndefVarError: c not defined

```julia
  for n_iter in 0:10
      if n_iter != 0
          println(n_iter, " ", c)
      end
      c = 2 
  end

```

To me this is a bit surprising (wouldn’t happen in python). What is the suggested workaround?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 16, 2018, 2:46pm UTC](https://discourse.julialang.org/t/strange-scope-issue/9759/2 "2018-03-16T14:46:55Z")

</div>

Have you read through [https://docs.julialang.org/en/stable/manual/variables-and-scoping/#scope-of-variables-1](https://docs.julialang.org/en/stable/manual/variables-and-scoping/#scope-of-variables-1)?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 16, 2018, 7:16pm UTC](https://discourse.julialang.org/t/strange-scope-issue/9759/3 "2018-03-16T19:16:31Z")

</div>

> [@ndbecker](#):
>
> What is the suggested workaround?

It’s not a workaround since this is not a bug, but you should either assign a value to `c` before the `for` loop or declare it with `local c`.
