# Function in array of functions not defined

**URL:** https://discourse.julialang.org/t/function-in-array-of-functions-not-defined/10758
**Category:** New to Julia
**Created:** [May 7, 2018, 11:07am UTC](https://discourse.julialang.org/t/function-in-array-of-functions-not-defined/10758 "2018-05-07T11:07:19Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Fatalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatalon/32/50663_2.png) [@Fatalon](https://discourse.julialang.org/u/Fatalon)
#### Post date: [May 7, 2018, 11:07am UTC](https://discourse.julialang.org/t/function-in-array-of-functions-not-defined/10758/1 "2018-05-07T11:07:19Z")

</div>

Sorry, I dont know if this question was answered already, but I wasn’t capable to find it.  
I want to iterate over data which I want to apply to different functions, collected in an Array. In this case, I want to apply the macro benchmark to it and call a function with the specific value and the specific function to create an entry in a dict.

```julia
valuesForRun = [10000,100000,1000000,10000000,100000000]
    functions::Array{Function} = [simple_loop_sum, threads_sum, sharedarray_parallel_sum, sharedarray_mapreduce, pmap_sum_nb, pmap_sum_b]
    for value in valuesForRun
        for i=1:length(functions)
            println(functions[i])
            println(typeof(functions[i]))
            fun::Function = functions[i]
            println(fun)
            run = @benchmark fun(value);
            createTestResultDict(run,fun,value);

```

I can iterate over data and functionArray and all println’s print the correct values, but if I try to call fun with a value, i get UndefVarError. Perhaps, there is easier way like map, also.

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 7, 2018, 11:22am UTC](https://discourse.julialang.org/t/function-in-array-of-functions-not-defined/10758/2 "2018-05-07T11:22:57Z")

</div>

Try interpolating the function, too. Your code modified as an MWE:

```julia
using BenchmarkTools
valuesForRun = 10.^(5:8)
dummy1(n) = 1+n
dummy2(n) = sum(sqrt.(1:n))
functions = [dummy1, dummy2]
for value in valuesForRun
    for f in functions
        println(f)
        println(typeof(f))
        run = @benchmark $f($value);
        println(run)
    end
end

```

Also, abstract type declarations are probably not buying you anything in terms of performance, so they can be omitted unless you have another reason to use them.

---

<div class="post-metadata">

### Author: ![Fatalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatalon/32/50663_2.png) [@Fatalon](https://discourse.julialang.org/u/Fatalon)
#### Post date: [May 8, 2018, 7:43am UTC](https://discourse.julialang.org/t/function-in-array-of-functions-not-defined/10758/3 "2018-05-08T07:43:37Z")

</div>

thank you for your advice,  
can you please explain, why splicing inside the macro is necessary? I thought the function was unquoted and therefore dont need to be explicitly evaluated. Am I wrong?

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [May 8, 2018, 9:51am UTC](https://discourse.julialang.org/t/function-in-array-of-functions-not-defined/10758/4 "2018-05-08T09:51:03Z")

</div>

See the [BenchmarkTools.jl manual on interpolation](https://github.com/JuliaCI/BenchmarkTools.jl/blob/master/doc/manual.md#interpolating-values-into-benchmark-expressions), especially this:

> Normally, you can’t use locally scoped variables in @benchmark or @benchmarkable, since all benchmarks are defined at the top-level scope by design. However, you can work around this by interpolating local variables into the benchmark expression:

and note that `f` is a local variable in your code.

---

<div class="post-metadata">

### Author: ![Fatalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatalon/32/50663_2.png) [@Fatalon](https://discourse.julialang.org/u/Fatalon)
#### Post date: [May 8, 2018, 4:40pm UTC](https://discourse.julialang.org/t/function-in-array-of-functions-not-defined/10758/5 "2018-05-08T16:40:59Z")

</div>

Thank your for your help

---

<div class="post-metadata">

### Author: ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)
#### Post date: [May 8, 2018, 7:08pm UTC](https://discourse.julialang.org/t/function-in-array-of-functions-not-defined/10758/6 "2018-05-08T19:08:27Z")

</div>

You are probably looking for [this](https://github.com/yuyichao/FunctionWrappers.jl).
