# How to define a vector of Function?

**URL:** <https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176>\
**Category:** General Usage\
**Created:** [August 14, 2024, 11:25am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176 "2024-08-14T11:25:53Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [August 14, 2024, 11:25am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/1 "2024-08-14T11:25:53Z")

</div>

I’m trying to define a `Vector{Function}` like this:

```julia
funvec = Vector{Function}(undef, 3);
for i in 1:3
    fun(theta) = (println(i); cos(theta[1] ^ 3) + 5 * (theta[2] ^ 2) + 10sin(theta[3]) )
    funvec[i] = fun
end

```

however, upon checking the whole vector contains only the last function:

```julia
julia> funvec[1]([0.1, 0.2, 0.3])
3
4.155201566613437

julia> funvec[2]([0.1, 0.2, 0.3])
3
4.155201566613437

julia> funvec[3]([0.1, 0.2, 0.3])
3
4.155201566613437

```

what’s the proper way to define _ **individual** _ functions?  
Thanks

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [August 14, 2024, 11:28am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/2 "2024-08-14T11:28:01Z")

</div>

Try replaicing `fun(theta) = ...` with `fun = theta -> ...`

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [August 14, 2024, 11:34am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/3 "2024-08-14T11:34:16Z")

</div>

what’s the syntax of function definition using `->` if I have more than one argument?

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [August 14, 2024, 11:34am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/4 "2024-08-14T11:34:52Z")

</div>

Your code actually already works for me (as does @DanielVandH 's suggested modification).

```julia-repl
julia> funvec[1](1:3)
1
21.95150238646681

julia> funvec[2](1:3)
2
21.95150238646681

julia> funvec[3](1:3)
3
21.95150238646681

```

> **versioninfo**
>
> ```julia-repl
> julia> versioninfo()
> Julia Version 1.10.4
> Commit 48d4fd4843 (2024-06-04 10:41 UTC)
> Build Info:
> Official https://julialang.org/ release
> Platform Info:
> OS: Windows (x86_64-w64-mingw32)
> CPU: 8 × Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
> WORD_SIZE: 64
> LIBM: libopenlibm
> LLVM: libLLVM-15.0.7 (ORCJIT, skylake)
> Threads: 8 default, 0 interactive, 4 GC (on 8 virtual cores)
> Environment:
> JULIA_NUM_THREADS = auto
> 
> ```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [August 14, 2024, 11:36am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/5 "2024-08-14T11:36:06Z")

</div>

I’m using 1.9.2 … but I don’t see changes in 1.10.x about this issue…

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [August 14, 2024, 11:39am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/6 "2024-08-14T11:39:11Z")

</div>

At the end of your loop, you have only left a single method of your function `fun`. Every new definition on the next iteration redefines (overwrites) the same method. That’s why your code does not work as intended.

The syntax suggested in not complete, use this:

```julia
funvec = Vector{Function}(undef, 3);
for i in 1:3
    fun = (theta) -> (println(i); cos(theta[1] ^ 3) + 5 * (theta[2] ^ 2) + 10sin(theta[3]) )
    funvec[i] = fun
end

```

Example for more than one argument:

```julia
(theta, x, y ) -> ...

```

---

<div class="post-metadata">

**Author:** ![DanielVandH](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielvandh/32/31134_2.png) [@DanielVandH](https://discourse.julialang.org/u/DanielVandH)\
**Post date:** [August 14, 2024, 11:39am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/7 "2024-08-14T11:39:14Z")

</div>

It’s the same. I just suspected that what’s happening is you’re overwriting the method for `fun(theta)` so maybe the anonymous function syntax would fix it.

e: Exactly as @oheil says

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [August 14, 2024, 11:39am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/8 "2024-08-14T11:39:56Z")

</div>

> [@tomtom](#):
>
> what’s the syntax of function definition using `->` if I have more than one argument?

You can just use brackets: e.g.

```julia-repl
julia> f = (x, y, z) -> x + y * z
#1 (generic function with 1 method)

julia> f(1, 2, 3)
7

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [August 14, 2024, 11:43am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/9 "2024-08-14T11:43:14Z")

</div>

with the `->` syntax now I can actually code directly like:

```julia
for i in 1:3
    funvec[i] = (theta) -> (println(i); cos(theta[1] ^ 3) + 5 * (theta[2] ^ 2) + 10sin(theta[3]) )
end

```

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [August 14, 2024, 11:55am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/10 "2024-08-14T11:55:42Z")

</div>

As an attempt to explain the difference between what you and I encounter if it’s not related to the Julia version, could it be you already had (accidentally) defined `fun` outside of the loop?

E.g.

```julia
fun(x) = 0

funvec = Vector{Function}(undef, 3)
for i = 1:3
    fun(x) = x * i
    funvec[i] = fun
end

println(funvec[1](1)) # prints 3
println(funvec[1] == fun == funvec[3]) # true

```

In that case the explanation of @oheil and @DanielVandH would certainly apply.

---

<div class="post-metadata">

**Author:** ![mohamed.d180](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed.d180/32/52028_2.png) [@mohamed.d180](https://discourse.julialang.org/u/mohamed.d180)\
**Post date:** [August 14, 2024, 11:59am UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/11 "2024-08-14T11:59:08Z")

</div>

I can confirm this is not issue in 1.10.4 version

 ![Screen Shot 2024-08-14 at 2.45.55 PM](https://global.discourse-cdn.com/julialang/original/3X/c/e/ce7de4831574d9547b610690c4c954175294e44a.png)

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [August 14, 2024, 12:01pm UTC](https://discourse.julialang.org/t/how-to-define-a-vector-of-function/118176/12 "2024-08-14T12:01:11Z")

</div>

> [@eldee](#):
>

U’re right. I should have accidentally defined `fun`.  
Thanks

Anyway, the `->` syntax is superior in the sense that I could directly assign the function definition into the vector.
