# How to create a vector of functions and index in a for loop

**URL:** https://discourse.julialang.org/t/how-to-create-a-vector-of-functions-and-index-in-a-for-loop/71107
**Category:** New to Julia
**Tags:** question, loops, vector, functions
**Created:** [November 7, 2021, 6:10pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-functions-and-index-in-a-for-loop/71107 "2021-11-07T18:10:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Sbeltranj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbeltranj/32/32827_2.png) [@Sbeltranj](https://discourse.julialang.org/u/Sbeltranj)
#### Post date: [November 7, 2021, 6:10pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-functions-and-index-in-a-for-loop/71107/1 "2021-11-07T18:10:43Z")

</div>

Hi, I want to create a vector of functions in julia, so that I can index in a for loop:

my code is:

```julia
qxloc1 = [qxloc(x) = 10
            qxloc(x) = 10]

qyloc2 = [qyloc(x) = 10
          qyloc(x) = 10]

for e = 1:2
    qxloc1[e]
             
end

```

I get the following error:

syntax: misplaced assignment statement in "[qxloc(x) = begin  
# REPL[20], line 1

---

<div class="post-metadata">

### Author: ![MatFi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matfi/32/10002_2.png) [@MatFi](https://discourse.julialang.org/u/MatFi)
#### Post date: [November 7, 2021, 7:11pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-functions-and-index-in-a-for-loop/71107/2 "2021-11-07T19:11:21Z")

</div>

```julia
qxloc1 = [ x -> 10
             x- > 10 ]

```

does the trick I guess…

---

<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: [November 7, 2021, 8:50pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-functions-and-index-in-a-for-loop/71107/3 "2021-11-07T20:50:04Z")

</div>

@Sbeltranj, it looks odd as example…  
Could you explain a bit further the objective?

An alternative to the anonymous functions in previous post:

```julia
qxloc(x) = 10
qxloc1 = [qxloc; qxloc]

qxloc1[1](5)
10

```

---

<div class="post-metadata">

### Author: ![John\_Gibson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/john_gibson/32/5321_2.png) [@John\_Gibson](https://discourse.julialang.org/u/John_Gibson)
#### Post date: [November 7, 2021, 10:23pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-functions-and-index-in-a-for-loop/71107/4 "2021-11-07T22:23:41Z")

</div>

```julia
julia> f(x) = x; g(x) = 2x; h(x) = 3x
h (generic function with 1 method)

julia> x = 1
1

julia> for ϕ in (f, g, h)
          @show ϕ(x)
       end
ϕ(x) = 1
ϕ(x) = 2
ϕ(x) = 3

julia> ϕ = [f, g, h]
3-element Vector{Function}:
 f (generic function with 1 method)
 g (generic function with 1 method)
 h (generic function with 1 method)

julia> for i in 1:3
          @show ϕ[i](x)
       end
(ϕ[i])(x) = 1
(ϕ[i])(x) = 2
(ϕ[i])(x) = 3

```
