# Metaprogramming: Nested function variable interpolation

**URL:** https://discourse.julialang.org/t/metaprogramming-nested-function-variable-interpolation/84940
**Category:** General Usage
**Tags:** question, metaprogramming
**Created:** [July 28, 2022, 9:27pm UTC](https://discourse.julialang.org/t/metaprogramming-nested-function-variable-interpolation/84940 "2022-07-28T21:27:02Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![RXGottlieb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rxgottlieb/32/23694_2.png) [@RXGottlieb](https://discourse.julialang.org/u/RXGottlieb)
#### Post date: [July 28, 2022, 9:27pm UTC](https://discourse.julialang.org/t/metaprogramming-nested-function-variable-interpolation/84940/1 "2022-07-28T21:27:02Z")

</div>

Hello,

I’m working on a piece of code that generates a math expression with embedded variables, and I want to generate a function that will take those variables as inputs and evaluate the math expression using the given inputs. E.g., if my math expression ends up being `a + b`, I want to have a generated function that takes `a` and `b` as inputs and will evaluate `a + b`. The purpose of this is that generating my math expression takes a lot of time, but once I have the math expression, evaluating it should be fast, and I want to be able to evaluate it with different variables a large number of times.

My thinking was to use a nested function approach where the variable names and final math expression are interpolated into an inner function definition, which hopefully I can later call to evaluate the equation. Here’s a contrived example of what I’m trying to do where the math expression is just a sum of `num` variables:

```julia
function make_func(num::Int)

    # Generate some variables
    vars = String[]
    for i = 1:num
        push!(vars, "v"*string(i))
    end

    # Generate an equation using these variables
    my_sum = ""
    for var in vars
        my_sum = my_sum * var * " + "
    end
    my_sum = my_sum[1:end-3]

    # Create a function that will evaluate this equation
    @eval begin
        function new_func(vars...)
            the_sum = Meta.parse($my_sum)
            return the_sum
        end
    end
end

```

Ideally, running `make_func(3)` would generate a function called `new_func` which takes 3 inputs, calculates the sum of the values, and returns the sum. But this doesn’t work:

```julia
julia> make_func(3); new_func(1,5,7)
:(v1 + v2 + v3)

julia> new_func()
:(v1 + v2 + v3)

```

For one thing, `new_func(vars...)` is clearly not creating a function where the elements of “vars” are the arguments. And for another thing, the `:(v1 + v2 + v3)` expression seems to be detached from the inputs to `new_func`, which means I can’t get its evaluation.

Is there any way to do something like this? Some fix to the nested function? A way to evaluate a code-generated expression multiple times without re-generating it?

Any help would be greatly appreciated!

Thanks!

---

<div class="post-metadata">

### Author: ![Julius\_Martensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julius_martensen/32/17077_2.png) [@Julius\_Martensen](https://discourse.julialang.org/u/Julius_Martensen)
#### Post date: [July 29, 2022, 10:15am UTC](https://discourse.julialang.org/t/metaprogramming-nested-function-variable-interpolation/84940/2 "2022-07-29T10:15:52Z")

</div>

You could just use `Symbolics.jl`

```julia
using Symbolics

function make_func(num::Int)
    # Generate some variables
    @variables v[1:num]
    v = collect(v)
    ex = sum(v)
    f_oop, _= build_function([ex], v..., expression = Val{false})
    f_oop
end

f_ = make_func(3)
f_(1,2,3)

```
