# Macro problem: Variable not defined

**URL:** <https://discourse.julialang.org/t/macro-problem-variable-not-defined/14697>\
**Category:** General Usage\
**Created:** [September 8, 2018, 8:56am UTC](https://discourse.julialang.org/t/macro-problem-variable-not-defined/14697 "2018-09-08T08:56:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [September 8, 2018, 8:56am UTC](https://discourse.julialang.org/t/macro-problem-variable-not-defined/14697/1 "2018-09-08T08:56:50Z")

</div>

This works:

```julia
using BenchmarkTools
fun = () -> sqrt(3)
@benchmark fun()

```

This however:

```julia
benchit(f) = @benchmark f()
benchit(fun)

```

Results in:

```julia
ERROR: UndefVarError: f not defined

```

Other macros work, e.g.:

```julia
timeit(f) = @time f()
timeit(fun)

```

Am I missing something, or is this a problem with the `@benchmark` macro? Perhaps it’s not escaping the expression correctly? If I create the following two macros:

```julia
macro m1(ex) :($(esc(ex))) end
macro m2(ex) :($ex) end

```

The first one works when called through a function like above, while the second one gives the same `ERROR: UndefVarError: f not defined`.

---

<div class="post-metadata">

**Author:** ![bennedich](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bennedich/32/4894_2.png) [@bennedich](https://discourse.julialang.org/u/bennedich)\
**Post date:** [September 8, 2018, 9:03am UTC](https://discourse.julialang.org/t/macro-problem-variable-not-defined/14697/2 "2018-09-08T09:03:58Z")

</div>

Ah, I think I found the solution:

```julia
benchit(f) = @benchmark $f()

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 8, 2018, 5:34pm UTC](https://discourse.julialang.org/t/macro-problem-variable-not-defined/14697/3 "2018-09-08T17:34:57Z")

</div>

Right, `@benchmark` operates at global scope, so any local values have to be interpolated in.
