# UndefVarError on a defined variable

**URL:** <https://discourse.julialang.org/t/undefvarerror-on-a-defined-variable/95686>\
**Category:** New to Julia\
**Created:** [March 7, 2023, 5:25pm UTC](https://discourse.julialang.org/t/undefvarerror-on-a-defined-variable/95686 "2023-03-07T17:25:10Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [March 7, 2023, 5:37pm UTC](https://discourse.julialang.org/t/undefvarerror-on-a-defined-variable/95686/2 "2023-03-07T17:37:56Z")

</div>

If you do not execute your script within the REPL, then there are strict rules about where variables are accessible.

The documentation about this is here [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

But there are also many posts in this forum where the behavior is explained. See for example [Undefined variable in loop - New to Julia - Julia Programming Language (julialang.org)](https://discourse.julialang.org/t/undefined-variable-in-loop/72936) for a more recent one.

* * *

In your case:

- It should work if you add the global keyword, e.g. `global b *= c[rand(1:3)]`. Note that your code does not update `b`, but just computes something without updating `b`.
- If you use VS Code or Atom/Juno, then you could run you code by selecting and “Shift + Enter”, in which case the rules for REPL apply which are a bit less strict.

---

_[View the full topic](https://discourse.julialang.org/t/undefvarerror-on-a-defined-variable/95686)._
