# How do I create a function from an expression

**URL:** <https://discourse.julialang.org/t/how-do-i-create-a-function-from-an-expression/12349>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 13, 2018, 4:38am UTC](https://discourse.julialang.org/t/how-do-i-create-a-function-from-an-expression/12349 "2018-07-13T04:38:40Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Chong\_Wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chong_wang/32/20307_2.png) [@Chong\_Wang](https://discourse.julialang.org/u/Chong_Wang)\
**Post date:** [July 13, 2018, 8:08am UTC](https://discourse.julialang.org/t/how-do-i-create-a-function-from-an-expression/12349/6 "2018-07-13T08:08:29Z")

</div>

But I think I found a fix

```julia
macro expr2fn(fname, expr, args...)
    fn = quote
        function $(esc(fname))()
            $(esc(expr.args[1]))
        end
    end
    for arg in args
        push!(fn.args[2].args[1].args, esc(arg))
    end
    return fn
end

@expr2fn(f, :(sin(x+y)), x, y)

julia> f(1.0, 1.0)
0.9092974268256817

```

I guess it’s just the documentation is never clear about what I can get if I pass a quoted expression into a macro.

---

_[View the full topic](https://discourse.julialang.org/t/how-do-i-create-a-function-from-an-expression/12349)._
