# Using Symbol within function (unexpected behaviour)

**URL:** https://discourse.julialang.org/t/using-symbol-within-function-unexpected-behaviour/24029
**Category:** New to Julia
**Created:** [May 9, 2019, 12:55pm UTC](https://discourse.julialang.org/t/using-symbol-within-function-unexpected-behaviour/24029 "2019-05-09T12:55:53Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dgs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgs/32/7015_2.png) [@dgs](https://discourse.julialang.org/u/dgs)
#### Post date: [May 9, 2019, 12:55pm UTC](https://discourse.julialang.org/t/using-symbol-within-function-unexpected-behaviour/24029/1 "2019-05-09T12:55:53Z")

</div>

Hello,

I am currently running some simulations and would like to serialize some parameters for the model.  
Using symbols I thought I could get away with a concise way of writing it :

```julia
c, d = 1, 2                                                                                                                                                                                                       
                                                                                                                                                                                              
serialize(open("save1.sl","w"),Dict(string(x)=>eval(x) for x=[:c,:d]))   

```

But ran into an unexpected behaviour when running the same code inside a function

```julia
using Serialization                                                                                                                                                                                         
                                                                                                                                                                                                            
function test()                                                                                                                                                                                             
    a ,b = 1                                                                                                                                                                                                                                                                                                                                                                      
    serialize(open("save2.sl","w"),Dict(string(x)=>eval(x) for x=[:a,:b])) # ERROR: LoadError: UndefVarError: a not defined                                                                                                                  
end                                                                                                                                                                                                         
 test() # fails a is undefined                                                                                                                                                                                                     

```

This seems like a trivial issue but I couldn’t find any help within the documentation…

Thanks in advance for your help!

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [May 9, 2019, 1:40pm UTC](https://discourse.julialang.org/t/using-symbol-within-function-unexpected-behaviour/24029/2 "2019-05-09T13:40:15Z")

</div>

`eval()` always operates in _global scope_. Check out the help text:

```nohighlight
help?> eval
...

  eval(expr)

  Evaluate an expression in the global scope of the containing module. ...

```

Since `eval()` operates in global scope, it does not have access to the values of your local variables.

Since Julia 1.1, though, we have the `Base.@locals()` macro, which gives you a dictionary mapping local variable names to their local values. Your use case might be a good fit for that:

```julia
julia> function foo()
         a = 1
         println(Base.@locals())
       end
foo (generic function with 1 method)

julia> foo()
Dict{Symbol,Any}(:a=>1)

```

---

<div class="post-metadata">

### Author: ![dgs](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dgs/32/7015_2.png) [@dgs](https://discourse.julialang.org/u/dgs)
#### Post date: [May 9, 2019, 3:39pm UTC](https://discourse.julialang.org/t/using-symbol-within-function-unexpected-behaviour/24029/3 "2019-05-09T15:39:37Z")

</div>

> [@rdeits](#):
>
> `Base.@locals()` macro, which gives you a dictionary mapping local variable names to their local values. Your use case might be a good fit for that:

Great! I completely missed out that eval was operating on the global scope. Thanks for your help!
