# Function variable scope in IJulia Jupyter notebook

**URL:** https://discourse.julialang.org/t/function-variable-scope-in-ijulia-jupyter-notebook/41792
**Category:** General Usage
**Tags:** question
**Created:** [June 20, 2020, 8:15pm UTC](https://discourse.julialang.org/t/function-variable-scope-in-ijulia-jupyter-notebook/41792 "2020-06-20T20:15:22Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Ken\_Lin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_lin/32/14367_2.png) [@Ken\_Lin](https://discourse.julialang.org/u/Ken_Lin)
#### Post date: [June 20, 2020, 8:15pm UTC](https://discourse.julialang.org/t/function-variable-scope-in-ijulia-jupyter-notebook/41792/1 "2020-06-20T20:15:22Z")

</div>

I’m using IJulia Jupyter notebook with Julia 1.3.1  
Say I define the following function:

```julia
function r(x)
    @show b
    @show a
    a = 1+x+b
    return a
end

```

And if I run the code below:

```julia
a = 5
b = 4
r(1)

```

I will get the output with an error:

```julia
b = 4
UndefVarError: a not defined

```

Could you please explain why `@show b` works while `@show a` doesn’t?

Thank you!

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [June 20, 2020, 9:37pm UTC](https://discourse.julialang.org/t/function-variable-scope-in-ijulia-jupyter-notebook/41792/2 "2020-06-20T21:37:30Z")

</div>

Since `a` is assigned in the code of the function, it is assumed to be a local variable, different from the outer local variable. On the other hand, `b` is used but never defined inside the function, so it is taken from the outer scope.

Details (for your version) here:  
[https://docs.julialang.org/en/v1.3/manual/variables-and-scoping/](https://docs.julialang.org/en/v1.3/manual/variables-and-scoping/)

---

<div class="post-metadata">

### Author: ![Ken\_Lin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ken_lin/32/14367_2.png) [@Ken\_Lin](https://discourse.julialang.org/u/Ken_Lin)
#### Post date: [June 30, 2020, 8:17pm UTC](https://discourse.julialang.org/t/function-variable-scope-in-ijulia-jupyter-notebook/41792/3 "2020-06-30T20:17:39Z")

</div>

Thank you!
