# Loop over the variable name inside of function

**URL:** <https://discourse.julialang.org/t/loop-over-the-variable-name-inside-of-function/73868>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [December 31, 2021, 3:21pm UTC](https://discourse.julialang.org/t/loop-over-the-variable-name-inside-of-function/73868 "2021-12-31T15:21:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eungik\_Lee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eungik_lee/32/22657_2.png) [@Eungik\_Lee](https://discourse.julialang.org/u/Eungik_Lee)\
**Post date:** [December 31, 2021, 3:21pm UTC](https://discourse.julialang.org/t/loop-over-the-variable-name-inside-of-function/73868/1 "2021-12-31T15:21:56Z")

</div>

Hi, I want to make a simple function with colnames x1, x2, …

So I made a below function and realized that “df” should be outside of the function.

Why the loop does not work if “df” is inside of the function?

I want to make a function including df inside.

Could someone help me?

Happy new year!

```julia
> function ASD(k::Int64)
> df=DataFrame()
> for i in 1:k
> @eval df.$(Symbol("x$i")) = randn(10)
> end
> 
> return df
> end
a=ASD(2)

```

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [December 31, 2021, 3:30pm UTC](https://discourse.julialang.org/t/loop-over-the-variable-name-inside-of-function/73868/2 "2021-12-31T15:30:01Z")

</div>

Welcome to the forum. I suggest you not using eval for nothing similar to that, it is not needed. You can use df[¡, name] to create a new column.  
You can directly use :auto.

```julia
df= DataFrame(rand(n, k), :auto)

```

I suggest to read the wonderful documentation.  
Happy new year!

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [December 31, 2021, 7:53pm UTC](https://discourse.julialang.org/t/loop-over-the-variable-name-inside-of-function/73868/3 "2021-12-31T19:53:49Z")

</div>

As to why, @eval works in the global scope. Routine advice is not to use @eval.
