# One-line function return lambda\_\_anonymous function

**URL:** <https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [June 4, 2020, 8:49pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762 "2020-06-04T20:49:37Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)\
**Post date:** [June 4, 2020, 8:49pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/1 "2020-06-04T20:49:37Z")

</div>

Hi,  
based on this

```julia
f = x -> x^2
f(10)

```

I’d like to develop a function with two inputs

```julia
f1(a,b) = a,b -> a^2+b
f1(2,1)

```

but this returns `(2, λ)`. What are the meaning of lambda returned here and the right syntax for this?

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [June 4, 2020, 9:04pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/2 "2020-06-04T21:04:01Z")

</div>

λ refers to the anonymous function b-\>a^2+b

Simply do

```julia
f1(a,b) = a^2+b

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [June 4, 2020, 9:07pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/3 "2020-06-04T21:07:51Z")

</div>

This also works if you want an anonymous function rather than a generic function.

```julia
julia> f1 = (a, b) -> a^2 + b
#3 (generic function with 1 method)

julia> f1(2, 1)
5

```

---

<div class="post-metadata">

**Author:** ![1634](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/1634/32/12119_2.png) [@1634](https://discourse.julialang.org/u/1634)\
**Post date:** [June 4, 2020, 9:22pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/4 "2020-06-04T21:22:02Z")

</div>

Oh I see

> julia\> f1(a,b) = a^2 + b  
> f1 (generic function with 1 method)
> 
> julia\> f2 = (a, b) → a^2 + b  
> #44 (generic function with 1 method)

But what is **anonymous function used for**? This seems not to be explained in its documentation  
[https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions-1](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions-1)

---

<div class="post-metadata">

**Author:** ![anon37204545](https://avatars.discourse-cdn.com/v4/letter/a/439d5e/32.png) [@anon37204545](https://discourse.julialang.org/u/anon37204545)\
**Post date:** [June 4, 2020, 9:22pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/5 "2020-06-04T21:22:10Z")

</div>

Just a little explanation, if you are curious. The code gets interpeted as

```julia
f1(a, b) = a, (b -> a^2+b)

```

which gets interpreted as

```julia
f1(a, b) = return a, (b -> a^2+b)

```

which produces the obvious error.

---

<div class="post-metadata">

**Author:** ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)\
**Post date:** [June 4, 2020, 9:30pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/6 "2020-06-04T21:30:40Z")

</div>

“Anonymous” literally means “without a name” and you use them whenever your function might not merit being named. For example, if I want to add 1.3 to each item of a list, defining `add1point3(x)` might seem a bit silly. So I can instead say `map(x -> x + 1.3, list)` and avoiding giving that operation a special name.

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [June 4, 2020, 9:31pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/7 "2020-06-04T21:31:34Z")

</div>

> [@1634](#):
>
> But what is **anonymous function used for**?

If you have a function that takes a function as an argument, it can more concise to define an anonymous function right in the outer function argument list.

A trivial example:

```julia
julia> d = rand(10)
10-element Array{Float64,1}:
 0.3551247514375906
 0.3340804663050978
 0.6544044171195078
 0.9508829086833299
 0.6444864931672418
 0.3549102821541359
 0.3284450308572071
 0.8639674871803269
 0.1763711097796281
 0.32769620811874867

julia> findfirst(x -> x>0.5, d)
3

```

---

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [June 4, 2020, 10:08pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/8 "2020-06-04T22:08:34Z")

</div>

There may be an implicit doubt in @1634’s question that remains to be answered: Ok, anonymous functions can be used as “throw-away functions” in arguments for `map`, `findfirst`, etc., without ever giving them a name. But what’s the difference between a “named” function and an anonymous one when it is (so to speak) “given” a name, i.e. assigned to a variable?

There are two notetworthy differences that I know of:

- Such variables are just containers that may be “emptied” or “reused” for another thing later on, unless they are declared as `const`, whereas function names _are_ constant by definition. For instance:

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

julia> foo(3)
9

julia> foo = 0
ERROR: invalid redefinition of constant foo
[...]

julia> bar = x -> x.^2
#7 (generic function with 1 method)

julia> bar(3)
9

julia> bar = 0
0

```

- Since they don’t have real names, I can’t define methods for different number or types of arguments. (If someone else does know how to do it, just excuse my ignorance!)

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [June 5, 2020, 12:25pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/9 "2020-06-05T12:25:21Z")

</div>

> [@heliosdrm](#):
>
> I can’t define methods for different number of types of arguments. (If someone else does know how to do it, just excuse my ignorance!)

There is no explicit syntax AFAIK, but one could easily get around this by defining a macro that converts

```julia
@multianon begin
    (a::Int, b::Int) = a + b
    (a::Float64, b::Float64) = a * b
end

```

to

```julia
let
    f(a::Int, b::Int) = a + b
    f(a::Float64, b::Float64) = a * b
    f
end

```

where `f` would be replaced by the macroexpander [hygienically](https://docs.julialang.org/en/v1/manual/metaprogramming/#Hygiene-1).

It’s just that there is little point in doing so.

---

<div class="post-metadata">

**Author:** ![pthariensflame](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pthariensflame/32/5633_2.png) [@pthariensflame](https://discourse.julialang.org/u/pthariensflame)\
**Post date:** [June 5, 2020, 1:32pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/10 "2020-06-05T13:32:55Z")

</div>

Can’t you do

```julia
foo = x -> …

(::typeof(foo))(x, y) = …

```

?

---

<div class="post-metadata">

**Author:** ![zdenek\_hurak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zdenek_hurak/32/53118_2.png) [@zdenek\_hurak](https://discourse.julialang.org/u/zdenek_hurak)\
**Post date:** [June 5, 2021, 10:10pm UTC](https://discourse.julialang.org/t/one-line-function-return-lambda-anonymous-function/40762/11 "2021-06-05T22:10:32Z")

</div>

Yes: [Documentation about using anonymous and named functions with pmap - #3 by baggepinnen](https://discourse.julialang.org/t/documentation-about-using-anonymous-and-named-functions-with-pmap/19337/3)
