# Same code different result in let block and begin block

**URL:** https://discourse.julialang.org/t/same-code-different-result-in-let-block-and-begin-block/86810
**Category:** General Usage
**Tags:** question
**Created:** [September 5, 2022, 4:44pm UTC](https://discourse.julialang.org/t/same-code-different-result-in-let-block-and-begin-block/86810 "2022-09-05T16:44:46Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![tiZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiz/32/211800_2.png) [@tiZ](https://discourse.julialang.org/u/tiZ)
#### Post date: [September 5, 2022, 4:44pm UTC](https://discourse.julialang.org/t/same-code-different-result-in-let-block-and-begin-block/86810/1 "2022-09-05T16:44:46Z")

</div>

Hi, I have same code in both `let` block and `begin` block, but get totally different results,  
any suggestion for understand this?

```julia
begin
    x = randn(100,10)
    function f()
        x = randn(2,2)
        return 1
    end
    a = f()
    println(size(x))
end
# (100,10)

let
    x = randn(100,10)
    function f()
        x = randn(2,2)
        return 1
    end
    a = f()
    println(size(x))
end
#(2,2)

```

why the `f()` in `let` block could change the value of x?, they are different scope, I think.

Thanks for your help.

---

<div class="post-metadata">

### Author: ![rmsmsgood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rmsmsgood/32/20544_2.png) [@rmsmsgood](https://discourse.julialang.org/u/rmsmsgood)
#### Post date: [September 5, 2022, 4:53pm UTC](https://discourse.julialang.org/t/same-code-different-result-in-let-block-and-begin-block/86810/2 "2022-09-05T16:53:43Z")

</div>

Maybe [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#man-scope-table) helps you.

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [September 5, 2022, 4:57pm UTC](https://discourse.julialang.org/t/same-code-different-result-in-let-block-and-begin-block/86810/3 "2022-09-05T16:57:31Z")

</div>

In the let block the function f captures the x which is in a local scope. But in the begin block x is in global scope so the function creates a new variable unless you tell it `global x = ...`

---

<div class="post-metadata">

### Author: ![tiZ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tiz/32/211800_2.png) [@tiZ](https://discourse.julialang.org/u/tiZ)
#### Post date: [September 5, 2022, 5:05pm UTC](https://discourse.julialang.org/t/same-code-different-result-in-let-block-and-begin-block/86810/4 "2022-09-05T17:05:56Z")

</div>

Thanks for your help. I read the URL before posting this question, but I used to think that the `x’ in the let and begin blocks were locally scoped.

And @dlakelan pointed out that `x’ in the begin block is globally scoped, now I can understand the difference result in my code.
