# Help understanding use of macros over functions that return expressions

**URL:** <https://discourse.julialang.org/t/help-understanding-use-of-macros-over-functions-that-return-expressions/51217>\
**Category:** General Usage\
**Tags:** question, macros, metaprogramming\
**Created:** [December 3, 2020, 6:36pm UTC](https://discourse.julialang.org/t/help-understanding-use-of-macros-over-functions-that-return-expressions/51217 "2020-12-03T18:36:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkarikom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkarikom/32/7096_2.png) [@mkarikom](https://discourse.julialang.org/u/mkarikom)\
**Post date:** [December 3, 2020, 6:36pm UTC](https://discourse.julialang.org/t/help-understanding-use-of-macros-over-functions-that-return-expressions/51217/1 "2020-12-03T18:36:25Z")

</div>

The docs are clear about how [macros execute before the enclosing code is run](https://docs.julialang.org/en/v1/manual/metaprogramming/#Hold-up:-why-macros?), but I’m still having trouble imagining where this is necessary, when we already have functions that return expressions.

It’s easy to come up with examples like `f_with_<>` below a macro like `@m_xy` does the same thing as a function like `f_xy`. Are there some alternative examples where macros would be necessary or provide some performance benefit?

```julia
macro m_xy(op,x,y)
        return Expr(:call,op,x,y)
end

function f_xy(op,x,y)
        return Expr(:call,op,x,y)
end

function f_with_mac()
        @m_xy(+,a*x,a*y)
end

function f_with_func()
        eval(f_xy(+,a*x,a*y))
end

a = 2
x = 3
y = 4
@test f_with_mac() == eval(f_with_func())

```

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [December 3, 2020, 6:51pm UTC](https://discourse.julialang.org/t/help-understanding-use-of-macros-over-functions-that-return-expressions/51217/2 "2020-12-03T18:51:50Z")

</div>

calling `eval` on an AST runs that code at global scope.  
Which has a bunch of problems.  
But especially if your macro is supposed to do things like delcare some local variables, then you can’t do that with `eval` (since then they would be global variables).
