# Index elements of a function with several outputs

**URL:** <https://discourse.julialang.org/t/index-elements-of-a-function-with-several-outputs/98684>\
**Category:** General Usage\
**Tags:** functions\
**Created:** [May 11, 2023, 2:49pm UTC](https://discourse.julialang.org/t/index-elements-of-a-function-with-several-outputs/98684 "2023-05-11T14:49:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ellocco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ellocco/32/31331_2.png) [@ellocco](https://discourse.julialang.org/u/ellocco)\
**Post date:** [May 11, 2023, 2:49pm UTC](https://discourse.julialang.org/t/index-elements-of-a-function-with-several-outputs/98684/1 "2023-05-11T14:49:51Z")

</div>

I would like to access specific output elements of a function.  
Here an example:

```julia
function foo()
    return 1, 2, 3, 4, 5
end

```

If I am only interested in output two and four I can use the underscore:

```julia
_, output2, _, output4, _ = foo()

```

If I am interested in outputs that are neighbors, I can write:

```julia
a, b = foo()[2:3]

```

In my case the function has 8 outputs and I am interested in the the first two and the last one, this looks as follows:

```julia
a, b, _, _, _, _, _, c = myfunction()

```

Is there something more elegant available?

---

<div class="post-metadata">

**Author:** ![barucden](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barucden/32/26154_2.png) [@barucden](https://discourse.julialang.org/u/barucden)\
**Post date:** [May 11, 2023, 3:02pm UTC](https://discourse.julialang.org/t/index-elements-of-a-function-with-several-outputs/98684/2 "2023-05-11T15:02:24Z")

</div>

You can index by a vector of indices:

```julia
julia> function foo()
           return 'a', 'b', 'c', 'd', 'e', 'f', 'g'
       end

julia> foo()[[3, 5, 1]]
('c', 'e', 'a')

```

I am, however, not sure if this is the best design. Instead, consider splitting your function into multiple smaller ones, each producing a single output, and then call only the ones you need. It is not always possible (or worth doing) though.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 11, 2023, 3:07pm UTC](https://discourse.julialang.org/t/index-elements-of-a-function-with-several-outputs/98684/3 "2023-05-11T15:07:38Z")

</div>

Another option would be to return a named tuple, if you have sensible names for the different outputs. For example:

```julia
function foo()
    return (a=1, b=2, c=3, d=4, e=5)
end

```

Then you can do, for example:

```julia
(; a, c, e) = foo()

```

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [May 11, 2023, 3:08pm UTC](https://discourse.julialang.org/t/index-elements-of-a-function-with-several-outputs/98684/4 "2023-05-11T15:08:51Z")

</div>

You can do something less pretty like

```julia
z = myfunction()
a,b,c = z[begin],z[begin+1],z[end]

```

I haven’t downloaded it yet on this machine, but as of v1.9 the following is allegedly supported (I have not verified, so it may not) thanks to [#42902](https://github.com/JuliaLang/julia/pull/42902):

```julia
a,b,_...,c = myfunction()

```
