# Optional output arguments

**URL:** <https://discourse.julialang.org/t/optional-output-arguments/139816>\
**Category:** New to Julia\
**Tags:** matlab\
**Created:** [October 4, 2026, 2:50pm UTC](https://discourse.julialang.org/t/optional-output-arguments/139816 "2026-10-04T14:50:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahmah](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mahmah/32/214326_2.png) [@mahmah](https://discourse.julialang.org/u/mahmah)\
**Post date:** [October 4, 2026, 2:50pm UTC](https://discourse.julialang.org/t/optional-output-arguments/139816/1 "2026-10-04T14:50:22Z")

</div>

A nice thing about MATLAB is that the caller can request multiple function outputs, i.e. you can either do:

```julia-auto
y = foo(5);

[y, info] = foo(5);

```

This is quite practical because you can then easily do `baz(bar(foo))` if info is not too important. I guess that this cannot be done in such a straightforward way in Julia. Is this correct?

---

<div class="post-metadata">

**Author:** ![linwaytin](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@linwaytin](https://discourse.julialang.org/u/linwaytin)\
**Post date:** [October 4, 2026, 3:07pm UTC](https://discourse.julialang.org/t/optional-output-arguments/139816/2 "2026-10-04T15:07:24Z")

</div>

You can also do

```julia-auto
y, _ = foo(5)

```

if you don’t want the second element.

---

<div class="post-metadata">

**Author:** ![adienes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adienes/32/37459_2.png) [@adienes](https://discourse.julialang.org/u/adienes)\
**Post date:** [October 4, 2026, 4:31pm UTC](https://discourse.julialang.org/t/optional-output-arguments/139816/3 "2026-10-04T16:31:25Z")

</div>

and it works with slurp as well

```julia-auto
y, _... = foo(5)

```

also, in slightly more cursed way, it also works if the assignment expression looks like a tuple unpacking even of a shorter length

```julia-auto
(y,) = foo(5)

```

---

<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:** [October 4, 2026, 4:44pm UTC](https://discourse.julialang.org/t/optional-output-arguments/139816/4 "2026-10-04T16:44:17Z")

</div>

It’s correct that Julia doesn’t provide any way for a function to introspect how many output values the caller will keep.

In case the extra outputs are free or cheap to compute, just return them and let the caller discard them, as proposed by other comments.

If the extra output is expensive and something you don’t want to spend time on unless it’s wanted, my preferred way is to add an extra _input_ with a mutable struct, vector, or `Ref` that the function can fill in with the desired information. E.g.

```julia-auto
function foo(x, info = nothing)
    y = ...
    if !isnothing(info)
        # Fill in `info`.
    end
    return y
end

```
