# Macro transforming a function call to a new function definition and call the new function

**URL:** <https://discourse.julialang.org/t/macro-transforming-a-function-call-to-a-new-function-definition-and-call-the-new-function/63100>\
**Category:** New to Julia\
**Tags:** macros\
**Created:** [June 17, 2021, 3:25pm UTC](https://discourse.julialang.org/t/macro-transforming-a-function-call-to-a-new-function-definition-and-call-the-new-function/63100 "2021-06-17T15:25:40Z")\
**Posts on this page:** 4\
**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:** [June 17, 2021, 3:25pm UTC](https://discourse.julialang.org/t/macro-transforming-a-function-call-to-a-new-function-definition-and-call-the-new-function/63100/1 "2021-06-17T15:25:40Z")

</div>

Continuing the discussion from [Make a variable as a global variable within a function](https://discourse.julialang.org/t/make-a-variable-as-a-global-variable-within-a-function/63067/20):

Since the original conversation is too long, I separated this question.

Suppose the below codes:

```julia
function _my_func(a, b)
    c = 2*a
    b = b + 1
    a + b
end

```

```julia
function my_func(a)
    c = nothing
    b = a
    @log _my_func(a, b)
    @show c
end

```

Then, the definition of `my_func` should be

```julia
function my_func(a)
    b = a
    c = nothing
    # the content of `_my_func`
    function _my_func __LOG__ (a, b)
        c = 2*a
        b = b + 1
        a+b
    end
    _my_func __LOG__ (a, b) # call the new function
    @show c # make it 2*a
end

```

Is it possible? If so, what should I do?

Background of this question: I will define a dictionary ` __LOGGER_DICT__ ` (in `my_func`) and update it in `_my_func`.

---

<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:** [June 17, 2021, 5:20pm UTC](https://discourse.julialang.org/t/macro-transforming-a-function-call-to-a-new-function-definition-and-call-the-new-function/63100/2 "2021-06-17T17:20:57Z")

</div>

In your function:

```julia
function _my_func(a, b)
    c = 2*a
    b = b + 1
    a + b
end

```

Both `a` and `b` are strictly local to the function, because they are given as arguments, so the reassignment to `b` in the second line will never affect any other `b` that is defined outside.

To give a counterexample, if this function is defined inside another local scope (e.g. inside a function), and `c` was also assigned in that outer local scope, the assignment of `c` in the first line would affect that other outer `c` (that’s the point of closures). This might be avoided by explicitly writing `local c = 2*a`.

So, in this particular example there is no need to rewrite the function with a macro, you could safely use `_my_func` inside `my_func`.

---

<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:** [June 17, 2021, 5:22pm UTC](https://discourse.julialang.org/t/macro-transforming-a-function-call-to-a-new-function-definition-and-call-the-new-function/63100/3 "2021-06-17T17:22:32Z")

</div>

~~You’re right. My example was wrong. It should be of variable `c`.  
I’ll modify the original question.~~ No, I was confused.  
The macro that I want to make is actually supposed to “change” the variable `c`!

---

<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:** [June 17, 2021, 5:24pm UTC](https://discourse.julialang.org/t/macro-transforming-a-function-call-to-a-new-function-definition-and-call-the-new-function/63100/4 "2021-06-17T17:24:01Z")

</div>

BTW, do you think it’s possible to rewrite a function with a macro as I desire?
