# I define t, then in a loop I'm told it's undefined, why?

**URL:** <https://discourse.julialang.org/t/i-define-t-then-in-a-loop-im-told-its-undefined-why/41258>\
**Category:** New to Julia\
**Created:** [June 12, 2020, 10:39am UTC](https://discourse.julialang.org/t/i-define-t-then-in-a-loop-im-told-its-undefined-why/41258 "2020-06-12T10:39:56Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [June 12, 2020, 10:47am UTC](https://discourse.julialang.org/t/i-define-t-then-in-a-loop-im-told-its-undefined-why/41258/2 "2020-06-12T10:47:42Z")

</div>

The content of a loop is a local scope, which does no have access to the global scope. The following should work, using `global`:

```julia
t = 0.0;
# The actual integration
for i=1:N
    global t
    t = t + Simpson(h,y,i,N);
    y = y + h;
end

```

The following works as well (notice that `tt` is defined outside the outer loop.

```julia
tt = 0
for j in 1:10
    for i in 1:10
        global tt
        tt += 1
    end
end

```

Within a function, `global` is not required. The loop will work fine.

```julia
function simpson()
    t = 0.0;
    # The actual integration
    for i=1:N   
       # `global` is not valid within a function
        t = t + Simpson(h,y,i,N);
        y = y + h;
    end
end

```

I do not know whether `global` refers just to the scope outside the loop, or to the function, or the entire global space.

---

_[View the full topic](https://discourse.julialang.org/t/i-define-t-then-in-a-loop-im-told-its-undefined-why/41258)._
