# Accessing only a subset of function outputs

**URL:** <https://discourse.julialang.org/t/accessing-only-a-subset-of-function-outputs/91558>\
**Category:** General Usage\
**Tags:** question, function, named-tuple\
**Created:** [December 12, 2022, 3:31pm UTC](https://discourse.julialang.org/t/accessing-only-a-subset-of-function-outputs/91558 "2022-12-12T15:31:21Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)\
**Post date:** [December 12, 2022, 3:31pm UTC](https://discourse.julialang.org/t/accessing-only-a-subset-of-function-outputs/91558/1 "2022-12-12T15:31:21Z")

</div>

suppose that there is a function with several return values

```julia
function foo() 
  ...
  ret1, ret2, ret3
end

```

and suppose that a caller is interested in a particular subset of the return values and not all of them.

How could this be implemented in an expressive manner?

My thoughts:

1. If only the first output is desired, then it is straightforward

2. if only one output is desired,

```julia
function foo() 
  ...
  (x = ret1, y = ret2, z = ret3)
end 

y = getindex(foo(),:y) # the caller does not need to store references to x and z in dummy variables 
                         # and implies that only y is going to be used 

```

I am wondering if there is still a better way to express this and how to handle case 3, i.e. if only accessing more than one output is desired.

---

<div class="post-metadata">

**Author:** ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)\
**Post date:** [December 12, 2022, 3:42pm UTC](https://discourse.julialang.org/t/accessing-only-a-subset-of-function-outputs/91558/2 "2022-12-12T15:42:00Z")

</div>

It is also possible to access a particular output in this way:

`y = foo()[2] # with the assumption that caller knows the order of the outputs`

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 12, 2022, 3:45pm UTC](https://discourse.julialang.org/t/accessing-only-a-subset-of-function-outputs/91558/3 "2022-12-12T15:45:44Z")

</div>

I like this:

```julia
julia> foo() = return 1,2,3
foo (generic function with 1 method)

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

julia> (_,x2,_)=foo();

julia> x2
2
```

---

<div class="post-metadata">

**Author:** ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)\
**Post date:** [December 12, 2022, 3:49pm UTC](https://discourse.julialang.org/t/accessing-only-a-subset-of-function-outputs/91558/4 "2022-12-12T15:49:34Z")

</div>

Does employment of named tuples for outputs come with any syntactical advantages in this context?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 12, 2022, 4:10pm UTC](https://discourse.julialang.org/t/accessing-only-a-subset-of-function-outputs/91558/5 "2022-12-12T16:10:38Z")

</div>

Depends on your problem.  
With named tuples you could do:

```julia
julia> foo() = return (x1=1,x2=2,x3=3)
foo (generic function with 1 method)

julia> r=foo();

julia> r.x1
1

```

I don’t think it gives anything worthy.

---

<div class="post-metadata">

**Author:** ![Atiyah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/atiyah/32/46462_2.png) [@Atiyah](https://discourse.julialang.org/u/Atiyah)\
**Post date:** [December 12, 2022, 4:28pm UTC](https://discourse.julialang.org/t/accessing-only-a-subset-of-function-outputs/91558/6 "2022-12-12T16:28:56Z")

</div>

> [@oheil](#):
>
> `julia> r=foo();`

This is clear. However, the idea is not to make non-used outputs accessible in the caller scope or use additional dummy references.

Named tuples would be beneficial if

- there are a lot of outputs
- no need to memorise the order of the outputs
- avoid potential errors (i.e. when confusing the order of the outputs of the same type)
- allow modification of the functions with additional outputs (without necessarily modifying the programs that calls such functions)
- …

---

<div class="post-metadata">

**Author:** ![mrufsvold](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mrufsvold/32/31600_2.png) [@mrufsvold](https://discourse.julialang.org/u/mrufsvold)\
**Post date:** [December 12, 2022, 4:36pm UTC](https://discourse.julialang.org/t/accessing-only-a-subset-of-function-outputs/91558/7 "2022-12-12T16:36:30Z")

</div>

I personally like NamedTuples for multiple returned values because if I’m refactoring later and need to add additional returned values, I don’t have to worry about how the order might affect every single call site. And should I later decide that the set of returned values is coherent enough to be a struct, I’ve already started accessing fields with properties so it’s easier to find and replace them.

But really, does something “come with any syntactical advantages” is really up to your experience of what is more readable and useful!

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [December 12, 2022, 4:49pm UTC](https://discourse.julialang.org/t/accessing-only-a-subset-of-function-outputs/91558/8 "2022-12-12T16:49:10Z")

</div>

Since Julia 1.7 you can use property destruction ([JuliaLang/julia#39285](https://github.com/JuliaLang/julia/pull/39285)) as follows:

```julia
julia> foo() = (x1 =1, x2 = 2, x3 = 3);

julia> (; x2) = foo();

julia> x2
2

```

* * *

> [@Atiyah](#):
>
> Named tuples would be beneficial if
> 
> - there are a lot of outputs
> - no need to memorise the order of the outputs

If your functions return too many things such that this is a problem I would rething the API and maybe break things down into smaller pieces.

> [@Atiyah](#):
>
> allow modification of the functions with additional outputs (without necessarily modifying the programs that calls such functions)

This is already possible (unless going from 1 to more return values), for example:

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

julia> _, b = foo();

julia> b
2

```
