# What's wrong here, why getting UndefVarError?

**URL:** <https://discourse.julialang.org/t/whats-wrong-here-why-getting-undefvarerror/20087>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [January 25, 2019, 12:27pm UTC](https://discourse.julialang.org/t/whats-wrong-here-why-getting-undefvarerror/20087 "2019-01-25T12:27:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![TimeMen](https://avatars.discourse-cdn.com/v4/letter/t/9de0a6/32.png) [@TimeMen](https://discourse.julialang.org/u/TimeMen)\
**Post date:** [January 25, 2019, 12:27pm UTC](https://discourse.julialang.org/t/whats-wrong-here-why-getting-undefvarerror/20087/1 "2019-01-25T12:27:17Z")

</div>

Hey there,  
I’m new to Julia and have some experiences with Python, but hate its slowness.

To get to know Julia, I wanted to resolve some problems from projecteuler, also to compare the time needed by Python and Julia.

The first problem isn’t that heavy and it needed only 6 lines in Python.  
But If I try to write the same algorithm Julia (using Atom), I get the following error:

```julia
> UndefVarError: var not defined

```

And I can’t figure out, where the mistake could be.  
This is the code I already have, and in theory, this should work, shouldn’t it?

```julia
#!/usr/bin/env julia
var = 0
for i = 1:1000
  if i % 3 == 0 || i % 5 == 0
    var += i
  end
end
println(var)

```

Why is my variable **var** undefinded, if I define it in first place?  
This seems for me quite illogical.

What’s my mistake, and what’s the actual reason that my version doesn’t work?

Best Wishes!  
TimeMen

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [January 25, 2019, 12:29pm UTC](https://discourse.julialang.org/t/whats-wrong-here-why-getting-undefvarerror/20087/2 "2019-01-25T12:29:47Z")

</div>

This works:

```julia
julia> for i = 1:1000
         if i % 3 == 0 || i % 5 == 0
           global var += i
         end
       end

```

This is the global scope issue, see [Another possible solution to the global scope debacle](https://discourse.julialang.org/t/another-possible-solution-to-the-global-scope-debacle/15894).

Also note that for speed, you need to do your computations inside a function. Where that loop will work just fine (without the `global`).
