# documentation of \`return nothing\` in Manual/Functions?

**URL:** <https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684>\
**Category:** Internals & Design\
**Created:** [May 3, 2018, 1:41pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684 "2018-05-03T13:41:31Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [May 3, 2018, 1:41pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/1 "2018-05-03T13:41:32Z")

</div>

Looking at the Julia 0.6 manual in the FAQ section [How does “null” or “nothingness” work in Julia?](https://docs.julialang.org/en/release-0.6/manual/faq/#How-does-%22null%22-or-%22nothingness%22-work-in-Julia?-1), I understand that a function returning nothing should use `return nothing`.

However, in the [The return Keyword](https://docs.julialang.org/en/release-0.6/manual/functions/#The-return-Keyword-1) section of the Manual/Functions page, I don’t see this. Should this convention be added here?

Best, Pierre

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 3, 2018, 2:05pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/2 "2018-05-03T14:05:05Z")

</div>

As is common with Lisp-like languages, the value of the last expression of a block is the value of a block, so `return` can be omitted in that place. Eg

```julia
function f(x, y)
    z = x + y
    z^2
end

function g(x, y)
    z = x + y
    return z^2
end

```

are equivalent.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [May 3, 2018, 2:33pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/3 "2018-05-03T14:33:53Z")

</div>

Yes, this is pretty clear from the Manual/Functions page. My question is only on the way the convention `return nothing` should be documented:

- only in the FAQ section ? (present situation, unles I missed something)
- also in the Manual/Function page?

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [May 3, 2018, 3:26pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/4 "2018-05-03T15:26:16Z")

</div>

Sure, I think that’d make a nice addition.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [May 28, 2018, 12:58pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/5 "2018-05-28T12:58:32Z")

</div>

Thanks for the feedback. I took some time to expand the Julia manual on Functions in this PR:

[https://github.com/JuliaLang/julia/pull/27286](https://github.com/JuliaLang/julia/pull/27286)

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 28, 2018, 1:09pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/6 "2018-05-28T13:09:29Z")

</div>

Remember though, that people also simply put `nothing` as the last expression, `return` as the last line of a function, or `return` when it is a conditional return.  
`return nothing` seems rather redundant, compared to simply having `return`.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [May 28, 2018, 1:22pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/7 "2018-05-28T13:22:01Z")

</div>

Thanks for sharing your experience with Julia. I created this thread because I was looking for the correct syntax, and it was difficult to find in the manual.

If I got it right, `return` returns `nothing` by default. As such, it would be a better recommandation than `return nothing` for the manual. Using `nothing` alone is an equally good option. Is that correct ?

---

<div class="post-metadata">

**Author:** ![jballanc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jballanc/32/328_2.png) [@jballanc](https://discourse.julialang.org/u/jballanc)\
**Post date:** [May 28, 2018, 2:40pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/8 "2018-05-28T14:40:23Z")

</div>

While this may not be on the level of “tabs vs spaces” or “vi(m) vs emacs”, every language I’ve used that has the property of everything being an expression ends up in a debate over whether or not to use the `return` keyword for explicit value returns. The typical argument for is that it is redundant, the typical argument against is that it’s better to be explicit so a random line with `nothing` at the end of a method isn’t mistaken for a bad merge or dangling paste. Personally, I favor the later argument. Similar arguments are made for/against significant whitespace, however, and that is no less settled of a debate (for the record, I am also anti-significant whitespace).

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 28, 2018, 2:53pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/9 "2018-05-28T14:53:12Z")

</div>

While no argument is likely to settle this issue, I just want to add my opinion as a datapoint: I strongly prefer explicit `return` statements, and find dangling expressions jarring, and a constant mental stumbling block, or at least they cause a mental hiccough every time. (The exception to this rule are short-form one-line functions.)

A plain `return` without `nothing` seems similarly confusing: Does it mean `return nothing`, or maybe it implies the new `missing`, or something completely different…

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [May 28, 2018, 4:21pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/10 "2018-05-28T16:21:40Z")

</div>

I didn’t think I would uncover a potentially heated debate!

Coming back to the original discussion, my goal is to _document_ Julia. So the question is more “is it allowed to do … ?” than “is good to do … ?”.

On that front, the fact that `nothing` alone is acceptable is self-evident from the “last value gets returned” rule, but the fact that `return` implicitly returns `nothing` is not so clear (except that it works when tested!). In particular, it is not in the doc of return. So maybe it should be added there as well?

By the way, I didn’t find the source for the doc of the `return` keyword (in fact the doc of keywords in general). Does anybody knows where it is in the source tree?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [May 28, 2018, 4:36pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/11 "2018-05-28T16:36:48Z")

</div>

> [@pierre-haessig](#):
>
> `return` implicitly returns `nothing` is not so clear (except that it works when tested!). In particular, it is not in the doc of return. So maybe it should be added there as well?

Other forms also implicitly return `nothing`, eg

```julia
if 2 < 1
    19
end

```

which, AFAICT, is also hard to find. Perhaps these could be documented in one place.

At the same time, I think that relying on the implicit `nothing` is bad style, but this is subjective of course. If there is agreement about this, it should be documented.

---

<div class="post-metadata">

**Author:** ![ScottPJones](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/scottpjones/32/146_2.png) [@ScottPJones](https://discourse.julialang.org/u/ScottPJones)\
**Post date:** [May 28, 2018, 9:22pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/12 "2018-05-28T21:22:45Z")

</div>

I guess coming from C/C++, that having `return` without an argument mean “return nothing” seems totally natural.  
I think it comes down to a matter of style, I only use `return` when it’s a non-local exit from the function,  
and explicitly put `nothing` as the last expression in the function instead of `return`.  
When I’m doing a PR for somebody else’s package, I try (if I don’t forget!) to use whatever style they seem to prefer in the rest of the code.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [May 28, 2018, 9:43pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/13 "2018-05-28T21:43:50Z")

</div>

> [@ScottPJones](#):
>
> I guess coming from C/C++, that having `return` without an argument mean “return nothing” seems totally natural.

I agree that it seems natural in that case that the function is “not returning anything”. But I think it’s less obvious that “not anything” is the same as `nothing`.

---

<div class="post-metadata">

**Author:** ![pierre-haessig](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pierre-haessig/32/217129_2.png) [@pierre-haessig](https://discourse.julialang.org/u/pierre-haessig)\
**Post date:** [May 30, 2018, 4:20pm UTC](https://discourse.julialang.org/t/documentation-of-return-nothing-in-manual-functions/10684/14 "2018-05-30T16:20:41Z")

</div>

I’ve updated the PR to show the three possible variants for returning nothing, along with a explanation that the choice between explicitness and compactness is a matter of style.

I’ve also added a small remark in the doc of the return keyword itself.
