# Evaluate Function Inside Quote

**URL:** <https://discourse.julialang.org/t/evaluate-function-inside-quote/10386>\
**Category:** General Usage\
**Tags:** metaprogramming\
**Created:** [April 17, 2018, 12:30pm UTC](https://discourse.julialang.org/t/evaluate-function-inside-quote/10386 "2018-04-17T12:30:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MLackner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlackner/32/2789_2.png) [@MLackner](https://discourse.julialang.org/u/MLackner)\
**Post date:** [April 17, 2018, 12:30pm UTC](https://discourse.julialang.org/t/evaluate-function-inside-quote/10386/1 "2018-04-17T12:30:28Z")

</div>

Hi! I’ve got a seemingly trivial problem to tackle…

Why does this work:

```julia
julia> function foo(x)
    f = quote
        function bar()
            a = $x
            println(a)
        end
    end
    eval(f)
end
foo (generic function with 1 method)

julia> foo(3)
bar (generic function with 1 method)

julia> bar()
3

```

And this doesn’t?

```julia
julia> baz(x) = $x
baz (generic function with 1 method)

julia> function foo2(x)
           f = quote
               function bar()
                   a = baz(x)
                   println(a)
               end
           end
           eval(f)
       end
foo2 (generic function with 1 method)

julia> foo2(1)
bar (generic function with 1 method)

julia> bar()
ERROR: error compiling bar: syntax: prefix "$" in non-quoted expression

```

Is there any way to get the behaviour of the first example with calling a function inside the quote?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [April 17, 2018, 12:35pm UTC](https://discourse.julialang.org/t/evaluate-function-inside-quote/10386/2 "2018-04-17T12:35:01Z")

</div>

When building an expression, `$(a)` is used to splice the current _value_ of `a` into that expression. The syntax `$(a)` cannot be used outside the expression (outside in the lexical sense).

The second example would be written as:

```julia
julia> baz(x) = x
baz (generic function with 1 method)

julia> function foo2(x)
           f = quote
               function bar()
                   a = $(baz(x))
                   println(a)
               end
           end
           eval(f)
       end

julia> foo2(1)
bar (generic function with 1 method)

julia> bar()
1

```

Here we splice the value of evaluating `baz(x)` into the expression.

---

<div class="post-metadata">

**Author:** ![MLackner](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mlackner/32/2789_2.png) [@MLackner](https://discourse.julialang.org/u/MLackner)\
**Post date:** [April 17, 2018, 1:35pm UTC](https://discourse.julialang.org/t/evaluate-function-inside-quote/10386/3 "2018-04-17T13:35:13Z")

</div>

That works perfectly. Thanks!
