# 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:** 1\
**Showing post:** 9

<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:** [October 5, 2026, 7:45am UTC](https://discourse.julialang.org/t/optional-output-arguments/139816/9 "2026-10-05T07:45:59Z")

</div>

> [@mahmah](#):
>
> I guess that this cannot be done in such a straightforward way in Julia. Is this correct?

See also previous discussions, which include a link to exactly this topic in the Julia manual: [Implement of Matlab function "nargout" in Julia](https://discourse.julialang.org/t/implement-of-matlab-function-nargout-in-julia/104011)

> [@GunnarFarneback](#):
>
> 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.

Two other options:

1. If you just do something that changes the input argument types, you can return an additional result by dispatching to a different method. For example, you could pass `WantsFooToo()` as an argument, where `WantsFooToo` is a singleton type, and it could dispatch to a different method that also computes and returns some data “foo”
2. You can return an object that computes the additional data on demand.

For an exampe, Julia’s linear-algebra factorizations often return a “factorization objection” that have overloaded `getproperty` to compute additional data, such as the explicit Q matrix from a QR factorization (which otherwise is stored implicitly).

Another example is reverse-mode AD systems like Zygote.jl and ChainRules.jl, where the adjoint rule is returned as a pullback function object that computes a vector–Jacobian product only if/when it is actually needed.

---

_[View the full topic](https://discourse.julialang.org/t/optional-output-arguments/139816)._
