# Macro: is it possible to receive a function and manipulate its arguments?

**URL:** https://discourse.julialang.org/t/macro-is-it-possible-to-receive-a-function-and-manipulate-its-arguments/56781
**Category:** General Usage
**Tags:** macros
**Created:** [March 9, 2021, 2:53am UTC](https://discourse.julialang.org/t/macro-is-it-possible-to-receive-a-function-and-manipulate-its-arguments/56781 "2021-03-09T02:53:40Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [March 9, 2021, 2:53am UTC](https://discourse.julialang.org/t/macro-is-it-possible-to-receive-a-function-and-manipulate-its-arguments/56781/1 "2021-03-09T02:53:40Z")

</div>

Hi. I’m a newbie of Julia macro.

I’d like to know whether it’s possible to get a function and manipulate its arguments by macro.  
For example, something like this.

```julia
@mymacro func(a, b) # == func(a+b, a-b)

```

---

<div class="post-metadata">

### Author: ![marius311](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marius311/32/3953_2.png) [@marius311](https://discourse.julialang.org/u/marius311)
#### Post date: [March 9, 2021, 3:05am UTC](https://discourse.julialang.org/t/macro-is-it-possible-to-receive-a-function-and-manipulate-its-arguments/56781/2 "2021-03-09T03:05:55Z")

</div>

Absolutely, the general way to go about writing macros is figure out what the expression tree looks like for the thing you’re trying to transform:

```julia
julia> ex = :(func(a,b))
:(func(a, b))

julia> dump(ex)
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol func
    2: Symbol a
    3: Symbol b

```

then figure out how to transform that `ex` into the thing you want and put it inside the macro,

```julia
macro mymacro(ex)
    # for your simple example above:
    esc(:($(ex.args[1])($(ex.args[2])+$(ex.args[3]), $(ex.args[2])-$(ex.args[3]))))
end

```

then check it did the right thing:

```julia
julia> @macroexpand @mymacro func(a,b)
:(func(a + b, a - b))

```

The MacroTools package has some convenient tools for matching and transforming expressions which are almost always useful, that can make writing that even more concise / readable (in particular look at `@capture`).

Not sure if your real case is more complex, but if not, note that there’s ways to do your simple example which don’t involve macros (and if you don’t _really_ need one, its almost always the right choice not to):

```julia
julia> func(a,b) = (a,b)
func (generic function with 1 method)

julia> sumdiff1(a, b) = (a+b, a-b)
sumdiff1 (generic function with 1 method)

julia> func(sumdiff1(1,2)...)
(3, -1)

```

or

```julia
julia> sumdiff2(f) = (a,b) -> f(a+b, a-b)
sumdiff2 (generic function with 1 method)

julia> sumdiff2(func)(1,2)
(3, -1)

```

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [March 9, 2021, 3:56am UTC](https://discourse.julialang.org/t/macro-is-it-possible-to-receive-a-function-and-manipulate-its-arguments/56781/3 "2021-03-09T03:56:15Z")

</div>

Thanks a lot! It’s a great explanation.

One more question: Can I use a macro to predefine some values for a certain structure?  
For example, I would like to set some values of struct `A` and reuse it ( **to avoid additional calculation** ) after the macro. Like this:

- What I want to do

```julia
my_value(a::A) == 1

```

- Assume that `@value A` is identical to what I want (Can a macro receive a type? I’m not sure)

```julia
@value A
a = A()
my_value(a) # == 1

```
