# Modify function with macros

**URL:** <https://discourse.julialang.org/t/modify-function-with-macros/64885>\
**Category:** General Usage\
**Tags:** question, macros\
**Created:** [July 18, 2021, 10:17pm UTC](https://discourse.julialang.org/t/modify-function-with-macros/64885 "2021-07-18T22:17:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alequa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alequa/32/12338_2.png) [@alequa](https://discourse.julialang.org/u/alequa)\
**Post date:** [July 18, 2021, 10:17pm UTC](https://discourse.julialang.org/t/modify-function-with-macros/64885/1 "2021-07-18T22:17:56Z")

</div>

Hi  
I would like to do modify a function inner scope with a macro.  
My goal is to add blocks of specific code within a simulation loop. These blocks have to:

- instantiate variables
- interact with internal variables
- update variables

My main function looks like this:

```julia
function main_loop(args..., kwargs...)
   x = 0. # an internal variable
   ## add block1

   for t in 1:1000
      ## add block2 
   end
end

```

My blocks will look like this:

```julia
#block1
foo = some_function(kwargs...)

#block2
x += t ## These variables are defined within the function only!
foo = some_other_function(x)

```

My idea would be something like:

```julia
macro add_code main_loop, args..., kwargs...)
   -> do the magic <-
end

```

I want to do this because I would like to define many of such blocks and apply them to the main simulation loop without replicating it.

```julia
macro add_code1 main_loop, args..., kwargs...)
   -> do the magic <-
end

macro add_code2 main_loop, args..., kwargs...)
   -> do the magic <-
end

new_function1 = @add_code1 main loop <stuff>

new_function2 = @add_code2 main loop <stuff>

```

Is this possible at all?  
Thanks

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 18, 2021, 10:20pm UTC](https://discourse.julialang.org/t/modify-function-with-macros/64885/2 "2021-07-18T22:20:43Z")

</div>

> [@alequa](#):
>
> My goal is to add blocks of specific code within a simulation loop.

Probably you don’t want a macro. Pass callback functions to your simulation loop instead.

---

<div class="post-metadata">

**Author:** ![alequa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alequa/32/12338_2.png) [@alequa](https://discourse.julialang.org/u/alequa)\
**Post date:** [July 18, 2021, 10:57pm UTC](https://discourse.julialang.org/t/modify-function-with-macros/64885/3 "2021-07-18T22:57:10Z")

</div>

yeah, but I have to write very crap code then.

Also, if different blocks define different variables - it is the case- then this is not effective.

I am thinking about defining the main loop as a quote and define place-holder variables that I can use to other pieces of code.  
Is this feasible?
