# Help with generating a function progamatically

**URL:** <https://discourse.julialang.org/t/help-with-generating-a-function-progamatically/32157>\
**Category:** New to Julia\
**Tags:** metaprogramming\
**Created:** [December 11, 2019, 3:50pm UTC](https://discourse.julialang.org/t/help-with-generating-a-function-progamatically/32157 "2019-12-11T15:50:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [December 11, 2019, 3:50pm UTC](https://discourse.julialang.org/t/help-with-generating-a-function-progamatically/32157/1 "2019-12-11T15:50:16Z")

</div>

Hi,

I’m trying to generate a function (or an expression which evaluates to function to be precise) where arguments are given by a vector of symbols, perhaps easiest explained by this example:

```julia
function fexpr()
vars = [:x,:y,:z]
quote
    function($(vars...))
        $(vars[1]) + $(vars[2]) * $(vars[3])
    end
end
end

```

The above fails (doesn’t compile) with the error message `ERROR: syntax: expected "(" in function definition`.

In case it is not clear, I would like the above to result in the same as this:

```julia
function fexpr()
vars = [:x,:y,:z]
quote
    function(x,y,z)
        $(vars[1]) + $(vars[2]) * $(vars[3])
    end
end
end

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [December 11, 2019, 6:28pm UTC](https://discourse.julialang.org/t/help-with-generating-a-function-progamatically/32157/2 "2019-12-11T18:28:04Z")

</div>

```julia
julia> vars = [:x, :y, :z]
3-element Array{Symbol,1}:
 :x
 :y
 :z

julia> :(function($(vars...)) end)
ERROR: syntax: expected "(" in function definition

julia> :(function($(vars...),) end)
:(function (x, y, z)
      #= REPL[3]:1 =#
  end)

```

---

<div class="post-metadata">

**Author:** ![DrChainsaw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/drchainsaw/32/8497_2.png) [@DrChainsaw](https://discourse.julialang.org/u/DrChainsaw)\
**Post date:** [December 12, 2019, 8:20am UTC](https://discourse.julialang.org/t/help-with-generating-a-function-progamatically/32157/3 "2019-12-12T08:20:07Z")

</div>

Thanks alot!

Just for my own education, what is the rationale behind the trailing comma? Is it just for disambiguation?
