documentation of `return nothing` in Manual/Functions?

Looking at the Julia 0.6 manual in the FAQ section How does “null” or “nothingness” work in Julia?, I understand that a function returning nothing should use return nothing.

However, in the The return Keyword section of the Manual/Functions page, I don’t see this. Should this convention be added here?

Best, Pierre

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

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

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

are equivalent.

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?
1 Like

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

2 Likes

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

1 Like

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.

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 ?

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).

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…

1 Like

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?

Other forms also implicitly return nothing, eg

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.

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.

3 Likes

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.

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.

1 Like