# Stumped on eval of function with parameters

**URL:** https://discourse.julialang.org/t/stumped-on-eval-of-function-with-parameters/34533
**Category:** New to Julia
**Tags:** metaprogramming
**Created:** [February 12, 2020, 6:51pm UTC](https://discourse.julialang.org/t/stumped-on-eval-of-function-with-parameters/34533 "2020-02-12T18:51:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![a5vzener](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/a5vzener/32/16257_2.png) [@a5vzener](https://discourse.julialang.org/u/a5vzener)
#### Post date: [February 12, 2020, 6:51pm UTC](https://discourse.julialang.org/t/stumped-on-eval-of-function-with-parameters/34533/1 "2020-02-12T18:51:18Z")

</div>

I am trying to provide a feature to allow a user to supply a function expression and have that expression evaluated at runtime.

The function needs two arguments, `m` and `n`. Both are assigned values before the evaluation.

```julia
function calculate(e::Expr)
   m = rand(1:4, 5, 5)
   n = rand(0:1, 5, 5)
   return eval(e)
end

```

Why does the following not work?

```julia
d = :(rank(m * n))
julia> calculate(d)
ERROR: UndefVarError: m not defined
Stacktrace:
 [1] top-level scope at none:0
 [2] eval at ./boot.jl:330 [inlined]
 [3] calculate(::Expr) at ./none:6
 [4] top-level scope at none:0

```

Thanks!

---

<div class="post-metadata">

### Author: ![pbayer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbayer/32/11675_2.png) [@pbayer](https://discourse.julialang.org/u/pbayer)
#### Post date: [February 12, 2020, 7:28pm UTC](https://discourse.julialang.org/t/stumped-on-eval-of-function-with-parameters/34533/2 "2020-02-12T19:28:33Z")

</div>

Why not use a function or function closure like that:

```julia
function calculate(f::Function)
    m = rand(1:4, 5, 5)
    n = rand(0:1, 5, 5)
    f(m, n) 
end

```

```julia
julia> using LinearAlgebra

julia> d(m,n) = rank(m*n)
d (generic function with 1 method)

julia> calculate(d)
4

julia> calculate((m,n)->rank(m*n))
4

```

wouldn’t that be faster, simpler and more idiomatic?

---

<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: [February 12, 2020, 7:39pm UTC](https://discourse.julialang.org/t/stumped-on-eval-of-function-with-parameters/34533/3 "2020-02-12T19:39:35Z")

</div>

The technical reason is that `eval` works in global scope. But as already been said, passing higher order functions is almost always the better API in cases like this.

---

<div class="post-metadata">

### Author: ![a5vzener](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/a5vzener/32/16257_2.png) [@a5vzener](https://discourse.julialang.org/u/a5vzener)
#### Post date: [February 12, 2020, 9:24pm UTC](https://discourse.julialang.org/t/stumped-on-eval-of-function-with-parameters/34533/4 "2020-02-12T21:24:25Z")

</div>

Yes! Thank you. Using Function directly as the type did the trick.

`calculate(reducef::Function = (m,n,f)->rank(m * n), ...) # this is the default function`  
…then…  
`reducef(m, n, f)`

@kristoffer.carlsson - I’m still not clear why eval() fails to work, even if I use eval(mymodule, expr). Are there built-in functions of Julia that can clarify the context in which code is evaluated at runtime? Very powerful stuff, this metaprogramming, but powerfully complicated as well. 😉
