# Return multiple values not in a tuple?

**URL:** https://discourse.julialang.org/t/return-multiple-values-not-in-a-tuple/45234
**Category:** General Usage
**Created:** [August 20, 2020, 12:03am UTC](https://discourse.julialang.org/t/return-multiple-values-not-in-a-tuple/45234 "2020-08-20T00:03:32Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)
#### Post date: [August 20, 2020, 12:03am UTC](https://discourse.julialang.org/t/return-multiple-values-not-in-a-tuple/45234/1 "2020-08-20T00:03:32Z")

</div>

I have a function that takes several arguments. I have another function that returns several values. I’d like the second function to be able to directly feed the first. But my attempt doesn’t work because multiple return values get bundled together in a tuple and my first function doesn’t expect a tuple. (And I don’t want it to, because in most contexts it won’t be eating the direct output of another function.)

Here’s a toy example

```julia
function test1(x,y,z)
   return x+y+z
end

function test2()
   return 1,2,3
end

julia> test1(test2())
ERROR: MethodError: no method matching test1(::Tuple{Int64,Int64,Int64})
Closest candidates are:
  test1(::Any, ::Any, ::Any) at REPL[15]:1
Stacktrace:
 [1] top-level scope at REPL[20]:1

```

Anyone have pointers on how to do this in an acceptly Julian way?

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [August 20, 2020, 12:05am UTC](https://discourse.julialang.org/t/return-multiple-values-not-in-a-tuple/45234/2 "2020-08-20T00:05:45Z")

</div>

Try `test1(test2()...)`. The `...` “splats” a collection, which is what you want to accomplish.

---

<div class="post-metadata">

### Author: ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)
#### Post date: [August 20, 2020, 1:11am UTC](https://discourse.julialang.org/t/return-multiple-values-not-in-a-tuple/45234/3 "2020-08-20T01:11:47Z")

</div>

Oh yeah, I forgot about the splat operator. I’m still a beginner, I guess.

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [August 20, 2020, 3:51am UTC](https://discourse.julialang.org/t/return-multiple-values-not-in-a-tuple/45234/4 "2020-08-20T03:51:59Z")

</div>

I know you said you don’t want to add a method to `test1` that accepts a tuple, but this can be done in a neat way using [argument destructuring](https://docs.julialang.org/en/v1/manual/functions/#Argument-destructuring-1):

```julia
test1(x, y, z) = x + y + z
test1((x, y, z)) = test1(x, y, z)

test2() = 1, 2, 3

```

```julia
julia> test1(test2())
6

```

---

<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: [August 20, 2020, 4:36am UTC](https://discourse.julialang.org/t/return-multiple-values-not-in-a-tuple/45234/5 "2020-08-20T04:36:03Z")

</div>

Or `test1(x) = test1(x...)`

(But I never understand when or whether the splatting has a performance penalty.)

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [August 20, 2020, 3:41pm UTC](https://discourse.julialang.org/t/return-multiple-values-not-in-a-tuple/45234/6 "2020-08-20T15:41:34Z")

</div>

> [@dpsanders](#):
>
> test1(x) = test1(x…)

Yeah, that has the advantage (or disadvantage, depending on the desired API) that it will throw a method error if you provide a tuple that is too long.

```julia
foo(x, y) = x + y
foo((x, y)) = foo(x, y)

bar(x, y) = x + y
bar(x) = bar(x...)

```

```julia
julia> foo((1, 2, 3))
3

julia> bar((1, 2, 3))
ERROR: MethodError: no method matching bar(::Int64, ::Int64, ::Int64)

```

---

<div class="post-metadata">

### Author: ![Gus\_Hart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gus_hart/32/6987_2.png) [@Gus\_Hart](https://discourse.julialang.org/u/Gus_Hart)
#### Post date: [August 25, 2020, 11:02pm UTC](https://discourse.julialang.org/t/return-multiple-values-not-in-a-tuple/45234/7 "2020-08-25T23:02:57Z")

</div>

Oh yeah. That’s a good solution too.

---

<div class="post-metadata">

### Author: ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)
#### Post date: [August 26, 2020, 4:13am UTC](https://discourse.julialang.org/t/return-multiple-values-not-in-a-tuple/45234/8 "2020-08-26T04:13:26Z")

</div>

> [@CameronBieganek](#):
>
> Yeah, that has the advantage (or disadvantage, depending on the desired API) that it will throw a method error if you provide a tuple that is too long.

This is relatively verbose, but if you prefer the argument destructuring approach:

```julia
julia> buz((x, y)::Tuple{Vararg{Any,2}}) = buz(x, y)
 buz (generic function with 2 methods)

julia> buz((1, 2.0))
 3.0

julia> buz((1, 2.0, 3))
ERROR: MethodError: no method matching buz(::Tuple{Int64,Float64,Int64})

```
