# "Assignment form" function definition following \`for\` keyword

**URL:** <https://discourse.julialang.org/t/assignment-form-function-definition-following-for-keyword/76786>\
**Category:** General Usage\
**Tags:** question, functions\
**Created:** [February 20, 2022, 7:30am UTC](https://discourse.julialang.org/t/assignment-form-function-definition-following-for-keyword/76786 "2022-02-20T07:30:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hiroharu\_Sugawara](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hiroharu_sugawara/32/9400_2.png) [@Hiroharu\_Sugawara](https://discourse.julialang.org/u/Hiroharu_Sugawara)\
**Post date:** [February 20, 2022, 7:30am UTC](https://discourse.julialang.org/t/assignment-form-function-definition-following-for-keyword/76786/1 "2022-02-20T07:30:38Z")

</div>

Hi folks. I find it weird that the following code works:

```julia
julia> begin
           for f(x)=2
               @show f(1)
               @show typeof(f)
           end
       end
f(1) = 2
typeof(f) = var"#f#1"{Int64}

```

I suppose the result of `f(x)=2` in the second line is not iterable, so `invalid iteration specification` error should occurs there.

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [February 20, 2022, 8:42am UTC](https://discourse.julialang.org/t/assignment-form-function-definition-following-for-keyword/76786/2 "2022-02-20T08:42:08Z")

</div>

I think it’s great that this works.

Compare

```julia
for f = (sin, cos, tan)
    @show f(1)
end

```

and

```julia
for x = 1
    @show x
end

```

I see no reason to special-case disallow the single assignment for loop just because the assignment in question is a function.

---

<div class="post-metadata">

**Author:** ![Hiroharu\_Sugawara](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hiroharu_sugawara/32/9400_2.png) [@Hiroharu\_Sugawara](https://discourse.julialang.org/u/Hiroharu_Sugawara)\
**Post date:** [February 20, 2022, 9:06am UTC](https://discourse.julialang.org/t/assignment-form-function-definition-following-for-keyword/76786/3 "2022-02-20T09:06:24Z")

</div>

Thanks for your reply. I also like iteration over functions.

You mean that the second line `for f(x)=2` in my first post is equivalent to `for f = [x->2]` in the following code ?  
I am confusing whether `f` in `for f(x)=2` is function name or loop control variable.

```julia
julia> begin
           for f = [x->2]
               @show f(1)
           end
       end
f(1) = 2

```

---

<div class="post-metadata">

**Author:** ![gustaphe](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gustaphe/32/18174_2.png) [@gustaphe](https://discourse.julialang.org/u/gustaphe)\
**Post date:** [February 20, 2022, 9:07am UTC](https://discourse.julialang.org/t/assignment-form-function-definition-following-for-keyword/76786/4 "2022-02-20T09:07:44Z")

</div>

> [@Hiroharu\_Sugawara](#):
>
> function name or loop control variable

Yes.
