# How to call a vector of functions?

**URL:** <https://discourse.julialang.org/t/how-to-call-a-vector-of-functions/87064>\
**Category:** General Usage\
**Tags:** question\
**Created:** [September 11, 2022, 6:47am UTC](https://discourse.julialang.org/t/how-to-call-a-vector-of-functions/87064 "2022-09-11T06:47:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [September 11, 2022, 6:47am UTC](https://discourse.julialang.org/t/how-to-call-a-vector-of-functions/87064/1 "2022-09-11T06:47:43Z")

</div>

```julia
julia> f(x)=x^2
f (generic function with 1 method)

julia> g(x)=x+1
g (generic function with 1 method)

julia> [f,g](3)
ERROR: MethodError: objects of type Vector{Function} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
 [1] top-level scope
   @ REPL[13]:1

julia> [f,g].(3)
ERROR: MethodError: objects of type Vector{Function} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
 [1] _broadcast_getindex_evalf
   @ .\broadcast.jl:670 [inlined]
 [2] _broadcast_getindex
   @ .\broadcast.jl:643 [inlined]
 [3] getindex
   @ .\broadcast.jl:597 [inlined]
 [4] copy
   @ .\broadcast.jl:875 [inlined]
 [5] materialize(bc::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0}, Nothing, Vector{Function}, Tuple{Int64}})
   @ Base.Broadcast .\broadcast.jl:860
 [6] top-level scope
   @ REPL[14]:1

julia>

```

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [September 11, 2022, 6:49am UTC](https://discourse.julialang.org/t/how-to-call-a-vector-of-functions/87064/2 "2022-09-11T06:49:05Z")

</div>

I would expect a result as [f(3),g(3)]. How to do that? Thanks.

---

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [September 11, 2022, 6:50am UTC](https://discourse.julialang.org/t/how-to-call-a-vector-of-functions/87064/3 "2022-09-11T06:50:51Z")

</div>

```julia
julia> [h(3) for h in [f,g]]
2-element Vector{Int64}:
 9
 4

```

---

<div class="post-metadata">

**Author:** ![photor](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/photor/32/14343_2.png) [@photor](https://discourse.julialang.org/u/photor)\
**Post date:** [September 11, 2022, 6:53am UTC](https://discourse.julialang.org/t/how-to-call-a-vector-of-functions/87064/4 "2022-09-11T06:53:30Z")

</div>

I myself found an answer:

```julia
julia> 3 .|> [f,g]
2-element Vector{Int64}:
 9
 4

julia>

```

---

<div class="post-metadata">

**Author:** ![qwerty](https://avatars.discourse-cdn.com/v4/letter/q/4491bb/32.png) [@qwerty](https://discourse.julialang.org/u/qwerty)\
**Post date:** [September 11, 2022, 7:35am UTC](https://discourse.julialang.org/t/how-to-call-a-vector-of-functions/87064/5 "2022-09-11T07:35:55Z")

</div>

```julia
using Tullio

f(x)=x^2
g(x)=x+1
k=[f,g]

@tullio a[i] := k[i](3)
println(a)

```

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [September 11, 2022, 9:04am UTC](https://discourse.julialang.org/t/how-to-call-a-vector-of-functions/87064/6 "2022-09-11T09:04:17Z")

</div>

Another way is to use `invoke` with broadcasting, like so:

```julia
julia> f(x)=x^2
f (generic function with 1 method)

julia> g(x)=x+1
g (generic function with 1 method)

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

julia> invoke.(v, Tuple{Int}, 3)
2-element Vector{Int64}:
 9
 4

```
