# Scope of a begin (and if) block

**URL:** <https://discourse.julialang.org/t/scope-of-a-begin-and-if-block/13811>\
**Category:** New to Julia\
**Tags:** question, scope\
**Created:** [August 21, 2018, 8:05am UTC](https://discourse.julialang.org/t/scope-of-a-begin-and-if-block/13811 "2018-08-21T08:05:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mtgd](https://avatars.discourse-cdn.com/v4/letter/m/3e96dc/32.png) [@mtgd](https://discourse.julialang.org/u/mtgd)\
**Post date:** [August 21, 2018, 8:05am UTC](https://discourse.julialang.org/t/scope-of-a-begin-and-if-block/13811/1 "2018-08-21T08:05:02Z")

</div>

Consider the following code, which results in `3` being printed:

```julia
x = 3
begin
    local x 
    x = 4
end
println(x)

```

In the documentation about Scope of Variables it says “the `begin` construct does not introduce a new scope”, and removing the `local x` line in the above code makes it behave exactly according to this. However, the begin block seems to introduce some kind of “invisible scope” that is turned into a kind of local scope if the `local` keyword is used.  
Further investigating this:

```julia
begin
    x = 3
    begin
        local x
        x = 4 # modifies x from outer begin-block
        y = -1 # global variable y
    end
    println(x)
end
println(y)

```

leads to `4` and `-1` being printed, so the scope introduced seems to default to the outer-most begin, and only apply to variables explicitly declared as local. Would it be correct to conclude `begin` (and similarly `if`) introduces a kind of “fall-through” scope (rather than no scope) that is ignored unless explicitly used?

I have a hard time understanding this behavior rigorously and can’t find any answers to this (arguably not very important question) online. Thanks in advance for any ideas about this!

For reference: I am using Julia 1.0 and refer to the corresponding documentation, but I suspect this to be similar in previous versions.

---

<div class="post-metadata">

**Author:** ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)\
**Post date:** [August 21, 2018, 8:44am UTC](https://discourse.julialang.org/t/scope-of-a-begin-and-if-block/13811/2 "2018-08-21T08:44:48Z")

</div>

I think this is a bug and should be reported. When writing macros, it is quite common to wrap things in `begin ... end` blocks or flatten such blocks away. And at least my assumption was that this would always lead to equivalent code.

---

<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:** [August 21, 2018, 9:09am UTC](https://discourse.julialang.org/t/scope-of-a-begin-and-if-block/13811/3 "2018-08-21T09:09:48Z")

</div>

X-ref: [`local` in global scope · Issue #10472 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/10472)
