# The zero-argument anonymous function name

**URL:** <https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025>\
**Category:** General Usage\
**Created:** [May 31, 2017, 10:58pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025 "2017-05-31T22:58:44Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [May 31, 2017, 10:58pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/1 "2017-05-31T22:58:44Z")

</div>

```julia
julia> bar(a,b,x...) = (a,b,x)
bar (generic function with 1 method)

julia> bar(1,2)
(1,2,())

```

Is `()` a zero-argument anonymous function that takes in varargs, making zero really mean any-argument anonymous function? If yes, then to me, the name zero-argument name just doesn’t fully capture/represent what it is. Anyone else feel this way?

If the zero-argument name changed to any-argument, I wouldn’t see the need to also use the name varargs. Or if zero changed to var-arguments, wouldn’t see the need to use zero and varargs.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 31, 2017, 11:05pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/2 "2017-05-31T23:05:06Z")

</div>

> [@hpoit](#):
>
> () a zero-argument anonymous function

No, it’s an empty tuple.

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [May 31, 2017, 11:08pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/3 "2017-05-31T23:08:23Z")

</div>

So can `()` take var-args?

Below, can `()->3` also take varargs?

```julia
julia> o = ()->3 # ()->3 officially named as zero-argument
(::#1) (generic function with 1 method)

julia> o()
3

```

Is `()` an empty tuple that takes var-args?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 31, 2017, 11:15pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/4 "2017-05-31T23:15:21Z")

</div>

The `()` in `()->3` is not a name. It is also akin to an empty tuple. Tuples represent the arguments of functions; an empty tuple means that the function takes no arguments.

`f() = 3` and `const f = ()->3` are basically defining the same thing.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 31, 2017, 11:20pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/5 "2017-05-31T23:20:53Z")

</div>

`()` is just parenthesis. In general it’s used when the syntax can otherwise be ambiguous.

In most cases it’s optional. You can write `x->x` instead of `(x)->x`, `x, y` instead of `(x, y)`. Zero element tuples and zero argument functions needs the `()` since it’ll be hard to represent otherwise.

And

> [@hpoit](#):
>
> Below, ()-\>3 can also take varargs, correct?

No, it’s wrong. You can try it yourself.

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [May 31, 2017, 11:21pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/6 "2017-05-31T23:21:13Z")

</div>

> [@mbauman](#):
>
> an empty tuple means that the function takes no arguments.

So `(1,2,())` means `bar` won’t take any other args other than `1,2`, that’s all it means?

```julia
julia> bar(a,b,x...) = (a,b,x)
bar (generic function with 1 method)

julia> bar(1,2)
(1,2,())

```

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 31, 2017, 11:22pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/7 "2017-05-31T23:22:02Z")

</div>

> [@hpoit](#):
>
> So (1,2,()) means bar won’t take any other args other than 1,2, that’s all it means?

No it means you didn’t give it “any other args other than 1, 2”

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [May 31, 2017, 11:25pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/8 "2017-05-31T23:25:22Z")

</div>

Okay, so it states the obvious, could’ve been `bar(1,2) => (1,2)`?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 31, 2017, 11:40pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/10 "2017-05-31T23:40:05Z")

</div>

Try `bar(1, 2, 3)` and `bar(1, 2, 3, 4, 5)` to understand what the splat (`...`) does.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [May 31, 2017, 11:40pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/11 "2017-05-31T23:40:07Z")

</div>

> [@hpoit](#):
>
> Okay, so it states the obvious, could’ve been bar(1,2) # =\> (1,2)?

No. for `bar(1, 2)` `x = ()`. Please read the full section of [https://docs.julialang.org/en/latest/manual/functions.html#Varargs-Functions-1](https://docs.julialang.org/en/latest/manual/functions.html#Varargs-Functions-1)

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [May 31, 2017, 11:52pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/12 "2017-05-31T23:52:25Z")

</div>

Yup, that exact Doc section led me here. Your previous comment about representing a zero element tuple drove the point home. It must be represented, even though there’s nothing you can do with it. Thanks.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [May 31, 2017, 11:59pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/13 "2017-05-31T23:59:10Z")

</div>

```julia
julia> x = ()
()

julia> typeof(x)
Tuple{}

julia> length(x)
0

```

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [June 1, 2017, 12:14am UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/14 "2017-06-01T00:14:19Z")

</div>

Why does `bar(1,2,3) => (1,2,(3,))`, but

`bar(1,2,3,4) => (1,2,(3,4))` instead of `=> (1,2,(3,4,))`?

---

<div class="post-metadata">

**Author:** ![Michael\_Eastwood](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_eastwood/32/669_2.png) [@Michael\_Eastwood](https://discourse.julialang.org/u/Michael_Eastwood)\
**Post date:** [June 1, 2017, 12:26am UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/15 "2017-06-01T00:26:54Z")

</div>

> [@hpoit](#):
>
> Why does bar(1,2,3) =\> (1,2,(3,)), but
> 
> bar(1,2,3,4) =\> (1,2,(3,4)) instead of =\> (1,2,(3,4,))?

Because one element tuples need the additional comma to distinguish them from regular parentheses. Two element tuples are not ambiguous so the extra comma is not required. For example:

```julia
julia> 1 # just an integer
1

julia> (1) # also just an integer
1

julia> (1,) # one element tuple
(1,)

julia> (1, 2) # two element tuple
(1,2)

julia> (1, 2,) # also a two element tuple
(1,2)

```

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [June 1, 2017, 1:36pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/17 "2017-06-01T13:36:49Z")

</div>

> [On the flip side, it is often handy to “splice” the values contained in an iterable collection into a function call as individual arguments. To do this, one also uses … but in the function call instead:](https://docs.julialang.org/en/stable/manual/functions/#varargs-functions)

```julia
x = (3,4);
bar(1,2,x...) => (1,2,(3,4))

```

How is it handy and why is splice quoted?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 1, 2017, 1:51pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/18 "2017-06-01T13:51:33Z")

</div>

> [@hpoit](#):
>
> How is it handy

Because you might want to do this?

> [@hpoit](#):
>
> why is splice quoted

Nothing is quoted. You are just passing more than one argument for the vararg part. It’s the same as `bar(1, 2, 3, 4)` so `x === (3, 4)` and `(a, b, x) === (1, 2, (3, 4))`.

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [June 1, 2017, 2:19pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/19 "2017-06-01T14:19:25Z")

</div>

How is it handy: can you give an example?

Why is splice quoted: I pasted the blockquote directly from the Julia Doc. Does splice here differ from splice in the original sense?

Despite the definition of `bar(a,b,x...) = (a,b,x)`, when `x = (2,3,4)`, why does calling `bar(1,x...) => (1,2,(3,4))` and not `=> (1,(2,3,4))`? Is the function definition assigning `b = 2`, first iterable of the collection `x`? If yes, what’s the point of that?

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 1, 2017, 3:48pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/20 "2017-06-01T15:48:16Z")

</div>

> [@hpoit](#):
>
> How is it handy: can you give an example?

You are already using it, i.e. `bar(1, 2, x...)`. As for why you need it in a program that depends on what you are writing.

> [@hpoit](#):
>
> I pasted the blockquote directly from the Julia Doc. Does splice here differ from splice in the original sense?

It’s unclear what you mean by splice and quote here.

> [@hpoit](#):
>
> Why does calling bar(1,x…) =\> (1,2,(3,4)) and not =\> (1,(2,3,4))

Because calling `bar(1, x...)` is identical with `bar(1, 2, 3, 4)`.

---

<div class="post-metadata">

**Author:** ![hpoit](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hpoit/32/245_2.png) [@hpoit](https://discourse.julialang.org/u/hpoit)\
**Post date:** [June 1, 2017, 5:29pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/21 "2017-06-01T17:29:40Z")

</div>

> [@yuyichao](#):
>
> As for why you need it in a program that depends on what you are writing.

How have you used it?

> [@yuyichao](#):
>
> It’s unclear what you mean by splice and quote here.

Why was it written in the Doc as

> On the flip side, it is often handy to “splice” the values contained

instead of On the flip side, it is often handy to splice the values contained?

> [@yuyichao](#):
>
> Because calling bar(1, x…) is identical with bar(1, 2, 3, 4).

I don’t follow. The explanation I came up with is that `bar(1,x...) => (1,2,(3,4))` where `x = (2,3,4)` is so because `bar(a,b,x...) = (a,b,x)` was defined as such, where `b` must be assigned to something. Is that a correct interpretation?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 1, 2017, 5:40pm UTC](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025/22 "2017-06-01T17:40:16Z")

</div>

Have you read [What does the … operator do?](https://docs.julialang.org/en/stable/manual/faq/#what-does-the-operator-do) in the FAQ?

[Next page](https://discourse.julialang.org/t/the-zero-argument-anonymous-function-name/4025.md?page=2)
