# Anonymous function as argument

**URL:** https://discourse.julialang.org/t/anonymous-function-as-argument/63473
**Category:** New to Julia
**Created:** [June 24, 2021, 12:11am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473 "2021-06-24T00:11:03Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Erasmo98](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erasmo98/32/26098_2.png) [@Erasmo98](https://discourse.julialang.org/u/Erasmo98)
#### Post date: [June 24, 2021, 12:11am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/1 "2021-06-24T00:11:03Z")

</div>

Why is common practice to use anonymous funtions where a normal function could be used instead? I’m specilly thinking when using it as an argument of another function. For example:

```julia
plot(0:0.1:2π, x -> sin(x))

```

I’ve seen code like that a lot but I don’t know why since

```julia
plot(0:0.1:2π, sin)

```

works perfectly fine.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [June 24, 2021, 12:28am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/2 "2021-06-24T00:28:01Z")

</div>

I haven’t seen that. Can you link to an example?

---

<div class="post-metadata">

### Author: ![Erasmo98](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erasmo98/32/26098_2.png) [@Erasmo98](https://discourse.julialang.org/u/Erasmo98)
#### Post date: [June 24, 2021, 12:50am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/3 "2021-06-24T00:50:36Z")

</div>

Basically is the same as my MWE but here is [this example from a Chaos course](https://github.com/lbenet/2021-2_TSelecFisComp-1/blob/main/clases/21-Mapeos.jl):

```julia
function analisis_grafico(f, x0::Float64, n::Int, domx=0.0:1/128:1.0)
    #Graficamos x->F(x) y x->x
    Plt1 = plot(domx, x->f(x),
        xaxis=("x", (domx[1], domx[end])),
        yaxis=("f(x)"),
        color=:blue, legend=false, grid=false)

    plot!(Plt1,
        domx, identity, color=:blue, linestyle=:dash)

    #Se grafican los iterados
    analisis_grafico!(Plt1, f, x0, n, domx)
    return Plt1
end

```

I’ve seen this in other contexts but I don’t remember them right now.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [June 24, 2021, 12:51am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/4 "2021-06-24T00:51:26Z")

</div>

I’d guess they just weren’t thinking about it.

---

<div class="post-metadata">

### Author: ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)
#### Post date: [June 24, 2021, 1:10am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/5 "2021-06-24T01:10:46Z")

</div>

Yeah, there’s no particular reason to do that, and it’s certainly not necessary. I’d guess that you might occasionally see it in a context where someone is trying to show more generally how an anonymous function /could/ be used, and they’ve just unintentionally chosen an example that could also be written without one.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [June 24, 2021, 1:29am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/6 "2021-06-24T01:29:59Z")

</div>

In that particular case that is not needed, but if they wanted to use an additional parameter for the function they could have done something like:

```julia
f(x,a) = a*x
function analisis_grafico(f, x0::Float64, n::Int, domx=0.0:1/128:1.0)
    a = 5.0
    Plt1 = plot(domx, x->f(x,a),
...

```

because the `plot` function is expecting a function with one variable, but you want to pass to it a function with two, one being a fixed parameter.

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [June 24, 2021, 1:35am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/7 "2021-06-24T01:35:14Z")

</div>

I have used something similar when the function `f` takes keyword arguments that I want to move away from the default values.

---

<div class="post-metadata">

### Author: ![Erasmo98](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erasmo98/32/26098_2.png) [@Erasmo98](https://discourse.julialang.org/u/Erasmo98)
#### Post date: [June 24, 2021, 2:51am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/8 "2021-06-24T02:51:11Z")

</div>

🤔

---

<div class="post-metadata">

### Author: ![Erasmo98](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erasmo98/32/26098_2.png) [@Erasmo98](https://discourse.julialang.org/u/Erasmo98)
#### Post date: [June 24, 2021, 2:56am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/9 "2021-06-24T02:56:05Z")

</div>

Yeah, I was also thinking that it would be necesary in the following example:

```julia
plot(0:0.1:2π, x -> sin(x^2))

```

---

<div class="post-metadata">

### Author: ![Erasmo98](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erasmo98/32/26098_2.png) [@Erasmo98](https://discourse.julialang.org/u/Erasmo98)
#### Post date: [June 24, 2021, 2:57am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/10 "2021-06-24T02:57:08Z")

</div>

Do you have an example?

---

<div class="post-metadata">

### Author: ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)
#### Post date: [June 24, 2021, 3:08am UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/11 "2021-06-24T03:08:01Z")

</div>

```julia
function f(x; exponent=2)
    sin(x^exponent)
end

plot(0:0.1:2π, x -> f(x; exponent=3))

```

---

<div class="post-metadata">

### Author: ![Erasmo98](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erasmo98/32/26098_2.png) [@Erasmo98](https://discourse.julialang.org/u/Erasmo98)
#### Post date: [June 25, 2021, 5:24pm UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/12 "2021-06-25T17:24:03Z")

</div>

Got it, thanks. (:

---

<div class="post-metadata">

### Author: ![Erasmo98](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erasmo98/32/26098_2.png) [@Erasmo98](https://discourse.julialang.org/u/Erasmo98)
#### Post date: [June 25, 2021, 5:30pm UTC](https://discourse.julialang.org/t/anonymous-function-as-argument/63473/13 "2021-06-25T17:30:41Z")

</div>

Thank you all for your replies. In the example I gave, there is no need to use an anonymous function but it is necessary to reduce the number of arguments of a function.
