# Using loop variable as an input for a macro function

**URL:** <https://discourse.julialang.org/t/using-loop-variable-as-an-input-for-a-macro-function/11516>\
**Category:** General Usage\
**Created:** [June 7, 2018, 6:26pm UTC](https://discourse.julialang.org/t/using-loop-variable-as-an-input-for-a-macro-function/11516 "2018-06-07T18:26:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bkang119](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@bkang119](https://discourse.julialang.org/u/bkang119)\
**Post date:** [June 7, 2018, 6:26pm UTC](https://discourse.julialang.org/t/using-loop-variable-as-an-input-for-a-macro-function/11516/1 "2018-06-07T18:26:10Z")

</div>

Hi, I have a simple question on macro function.

Let say I have a macro function

```julia
macro add_one(i::Int64)
  i+1
end

```

which does the simple thing:

```julia
@add_one 1

```

gives `2` as expected. Then I want to use the function inside the for loop in the following way:

```julia
for i=1:100
  @add_one i 
end   

```

then, I have an error:

```julia
MethodError: no method matching @add_one(::Symbol)
Closest candidates are:
  @add_one(::Int64) at Ind[94]:2

```

It seems like I cannot use `i`, the for-loop variable, as an argument of a macro function. Are there any ways to use `i` as an input for `@add_one`?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [June 7, 2018, 6:33pm UTC](https://discourse.julialang.org/t/using-loop-variable-as-an-input-for-a-macro-function/11516/2 "2018-06-07T18:33:44Z")

</div>

When you write a macro, the types it sees are the types of the _code_ that’s provided to it. This is because macros are evaluated at compile time. The literal `1` is indeed just an `Int64`, but `i` is a `Symbol`. Try doing

```julia
macro add_one(i)
    :($i+1)
end

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 7, 2018, 6:43pm UTC](https://discourse.julialang.org/t/using-loop-variable-as-an-input-for-a-macro-function/11516/3 "2018-06-07T18:43:08Z")

</div>

> macro function

Macros are not functions.

> [@ExpandingMan](#):
>
> :($i+1)

`:($(esc(i)) + 1)`

---

<div class="post-metadata">

**Author:** ![bkang119](https://avatars.discourse-cdn.com/v4/letter/b/a3d4f5/32.png) [@bkang119](https://discourse.julialang.org/u/bkang119)\
**Post date:** [June 7, 2018, 6:45pm UTC](https://discourse.julialang.org/t/using-loop-variable-as-an-input-for-a-macro-function/11516/4 "2018-06-07T18:45:16Z")

</div>

This works!!! thanks for your comments. Thanks to ExpandingMan as well!
