# On loop scoping, inside functions

**URL:** https://discourse.julialang.org/t/on-loop-scoping-inside-functions/68201
**Category:** General Usage
**Tags:** scope
**Created:** [September 15, 2021, 9:29am UTC](https://discourse.julialang.org/t/on-loop-scoping-inside-functions/68201 "2021-09-15T09:29:10Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)
#### Post date: [September 15, 2021, 9:29am UTC](https://discourse.julialang.org/t/on-loop-scoping-inside-functions/68201/1 "2021-09-15T09:29:10Z")

</div>

I know, this topic again. After a year I thought I understood this well enough, I was wrong. I expect this to work but it doesn’t. I don’t want to declare b as a global here, just access it inside the function.

```julia
function foo()
    for i in 1:10
        b=rand()
    end
    print(b)
end

```

But it works if I initialize it…why?

```julia
function bar()
    b=undef
    for i in 1:10
        b=rand()
    end
    print(b)
end

```

---

<div class="post-metadata">

### Author: ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)
#### Post date: [September 15, 2021, 10:08am UTC](https://discourse.julialang.org/t/on-loop-scoping-inside-functions/68201/2 "2021-09-15T10:08:01Z")

</div>

I see how this particular example is not covered in [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/) , but if you look closely into the local scope section, there’s an example that starts with

> Let’s dig into the fact that the `for` loop body has its own scope for a second…

You see there, that in the for loop a new local variable `t` is defined. The same is happening in your example, where you define a new variable `b` local to the for loop in each iteration. Then, similar to `t` in the documentation, it is not available outside the loop.

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [September 15, 2021, 11:26am UTC](https://discourse.julialang.org/t/on-loop-scoping-inside-functions/68201/3 "2021-09-15T11:26:10Z")

</div>

You can also use `local b` instead of `b = undef` before executing the loop. This has the advantage of a linter in your IDE being able to tell you that `b` isn’t assigned to one some path, should that be the case. You won’t run into the problem of spuriously seeing some `undef` somewhere you didn’t expect.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [September 15, 2021, 11:54am UTC](https://discourse.julialang.org/t/on-loop-scoping-inside-functions/68201/4 "2021-09-15T11:54:06Z")

</div>

> [@levasco](#):
>
> `b=undef`

I wonder if this has performance hit because of type instability, compare to `local`
