# Repeated section of code

**URL:** https://discourse.julialang.org/t/repeated-section-of-code/68157
**Category:** New to Julia
**Tags:** macros
**Created:** [September 14, 2021, 5:36pm UTC](https://discourse.julialang.org/t/repeated-section-of-code/68157 "2021-09-14T17:36:59Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [September 14, 2021, 5:36pm UTC](https://discourse.julialang.org/t/repeated-section-of-code/68157/1 "2021-09-14T17:36:59Z")

</div>

Hello,  
I have certain parts of my code that show up in a lot of functions. They are simple blocks of math like:

```julia
a=x+2
b=a+3
...

```

And `a` and `b` are later used in different ways. One way of doing this is like this:

```julia
function fun(x)
a=x+2
b=a+3
return a,b
# later in the code
a,b=fun(x)

```

but this gets cumbersome fast, as I have a lot of variables that are used and defined, plus a lot of them are StaticArrays and the whole thing gets a bit messy.

Is there some command to tell the compiler to insert a block of code into a function and that everything that gets declared/modified in that block is later seen by the function? Not with global variables, as most of these chunks create local variables that are used within parallelized for loops.  
Performance is important.  
Thanks!

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [September 14, 2021, 5:44pm UTC](https://discourse.julialang.org/t/repeated-section-of-code/68157/2 "2021-09-14T17:44:33Z")

</div>

You can do this with [macros](https://docs.julialang.org/en/v1/manual/metaprogramming/#man-macros). But I would personally find such a solution much _messier_ than a proper use of functions. I think it’s much better to use functions, and if you have many variables that are used in many places, consider using named tuples or custom struct types.

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [September 14, 2021, 6:06pm UTC](https://discourse.julialang.org/t/repeated-section-of-code/68157/3 "2021-09-14T18:06:15Z")

</div>

Thanks. I couldn’t figure out a way to have a macro that puts variables in the scope where it’s called. I know you can make a variable in a macro `global`, but what I’d like is something like:

```julia
function fun(x)
  for i=1:N
    @mac(x)
    vec[i] = a+b
  end
end
macro mac(x)
  a=2*x
  b=3*a
end

```

where `a` and `b` are available to use within the scope of the for loop, but not outside of it (and the for loop can be multi threaded).  
Is this doable?

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [September 14, 2021, 7:08pm UTC](https://discourse.julialang.org/t/repeated-section-of-code/68157/4 "2021-09-14T19:08:53Z")

</div>

Linking this [related thread](https://discourse.julialang.org/t/how-to-include-into-local-scope/34634).

---

<div class="post-metadata">

### Author: ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)
#### Post date: [September 14, 2021, 7:58pm UTC](https://discourse.julialang.org/t/repeated-section-of-code/68157/5 "2021-09-14T19:58:20Z")

</div>

Maybe you wanted to define the macro as follows:

```julia
macro mac(x)
    quote
        a=2*$x
        b=3*a
    end |> esc
end

```

---

<div class="post-metadata">

### Author: ![Ribeiro](https://avatars.discourse-cdn.com/v4/letter/r/d9b06d/32.png) [@Ribeiro](https://discourse.julialang.org/u/Ribeiro)
#### Post date: [September 14, 2021, 9:17pm UTC](https://discourse.julialang.org/t/repeated-section-of-code/68157/6 "2021-09-14T21:17:20Z")

</div>

Works like a charm and it’s fast! Thanks!
