# "UndefVarError: V not defined" using simple loop

**URL:** <https://discourse.julialang.org/t/undefvarerror-v-not-defined-using-simple-loop/74566>\
**Category:** New to Julia\
**Tags:** scope\
**Created:** [January 13, 2022, 5:03pm UTC](https://discourse.julialang.org/t/undefvarerror-v-not-defined-using-simple-loop/74566 "2022-01-13T17:03:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tadas\_Surkys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tadas_surkys/32/32754_2.png) [@Tadas\_Surkys](https://discourse.julialang.org/u/Tadas_Surkys)\
**Post date:** [January 13, 2022, 5:03pm UTC](https://discourse.julialang.org/t/undefvarerror-v-not-defined-using-simple-loop/74566/1 "2022-01-13T17:03:40Z")

</div>

I stuck in my very first lines. Why V is not visible inside the loop? What is the logic behind that?

> V=1  
> for i in 1:2  
> print(V)  
> V=2  
> end

ps Matlab background

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 13, 2022, 5:10pm UTC](https://discourse.julialang.org/t/undefvarerror-v-not-defined-using-simple-loop/74566/2 "2022-01-13T17:10:55Z")

</div>

I recommend working within the REPL where you have global softscope.

```julia
julia> V=1
       for i in 1:2
       print(V)
       V=2
       end
12

```

Outside of the REPL, you need to declare V as a global within the for loop. Alternatively and preferably, execute this within a function.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 13, 2022, 5:12pm UTC](https://discourse.julialang.org/t/undefvarerror-v-not-defined-using-simple-loop/74566/3 "2022-01-13T17:12:53Z")

</div>

Here is how you can explicitly declare a global within the for loop. Note this is not recommended and is a recipe for slow code.

```julia
PS C:\Users\kittisopikulm> julia -e'
>> V=1
>> for i in 1:2
>> print(V)
>> V=2
>> end
>> '
┌ Warning: Assignment to `V` in soft scope is ambiguous because a global variable by the same name exists: `V` will be treated as a new local. Disambiguate by using `local V` to suppress this warning or `global V` to assign to the existing global variable.
└ @ none:5
ERROR: UndefVarError: V not defined
Stacktrace:
 [1] top-level scope
   @ .\none:4
PS C:\Users\kittisopikulm>
PS C:\Users\kittisopikulm> julia -e'
>> V=1
>> for i in 1:2
>> global V
>> print(V)
>> V=2
>> end
>> '
12

```

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [January 13, 2022, 5:15pm UTC](https://discourse.julialang.org/t/undefvarerror-v-not-defined-using-simple-loop/74566/4 "2022-01-13T17:15:40Z")

</div>

If you put everything into a function, then this works within the function’s local scope.

```julia
PS C:\Users\kittisopikulm> julia -e'
>> function foo()
>>
>> V=1
>> for i in 1:2
>> print(V)
>> V=2
>> end
>>
>> end
>>
>> foo()
>> '
12

```

For more information, see [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/)

---

<div class="post-metadata">

**Author:** ![Tadas\_Surkys](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tadas_surkys/32/32754_2.png) [@Tadas\_Surkys](https://discourse.julialang.org/u/Tadas_Surkys)\
**Post date:** [January 13, 2022, 6:03pm UTC](https://discourse.julialang.org/t/undefvarerror-v-not-defined-using-simple-loop/74566/5 "2022-01-13T18:03:49Z")

</div>

Thank you for instant answer.
