# How does Julia parse \`return\`?

**URL:** https://discourse.julialang.org/t/how-does-julia-parse-return/123044
**Category:** New to Julia
**Tags:** question
**Created:** [November 25, 2024, 12:10pm UTC](https://discourse.julialang.org/t/how-does-julia-parse-return/123044 "2024-11-25T12:10:04Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)
#### Post date: [November 25, 2024, 12:10pm UTC](https://discourse.julialang.org/t/how-does-julia-parse-return/123044/1 "2024-11-25T12:10:05Z")

</div>

This question comes about after I had been using the JuliaFormatter.jl package to format some existing code.

Compare the following 4 functions:

```julia
function example()
    return
end

```

```julia
function example()
    return
    if a == 1
        b = 2
    end
    return otherFunction(b)
end

```

```julia
function example()
    return a == 1 && otherFunction(4)
end

```

```julia
function example()
    return if a == 1
        otherFunction(4)
    else
        false
    end
end

```

In the first example, this is a return statement which returns `nothing`.

The second example is a bit harder to read. It depends on how `return` is parsed.

This is my _guess_: (I could not find anything about this in the documentation.)

- `return` is a single line statement. The statement is implicitly ended by a new line.

If the above is correct, then the second example will `return nothing`, everytime. The `if` statement which follows is not part of the `return` statement.

The third example is tricky to read too. It returns `false` if `a != 1`, and otherwise it returns the value produced by the function call `otherFunction(4)`.

The fourth example is exactly the same as the third. But, an `if` statement begins on the same line as the `return` statement. That makes it different from the second example, and also different from the third example.

- Now, the `return` statement is not ended until the `if` statement is completed by the associated `end`. So in this case, a `return` statement can be multi-line.

It seems to be the same as

```julia
return (
    if a == 1 otherFunction(4) else false end
)

```

Is anyone able to clarify the parsing rules relating to `return`, and also let me know if anything I have written here is not correct?

---

<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: [November 25, 2024, 12:52pm UTC](https://discourse.julialang.org/t/how-does-julia-parse-return/123044/2 "2024-11-25T12:52:28Z")

</div>

The questions you’re raising are about parsing expressions in general, not just `return`! `\n` does terminate an expression. `return` returns the value to which it’s expression evaluated.

You can investigate the structure of an expression with the `dump` function, like `dump(:(#=paste your function or any expression here=#))`

When run in the REPL, this “dumps” what’s the internal `Expr` representation that Julia uses for your expression.

---

<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: [November 25, 2024, 1:45pm UTC](https://discourse.julialang.org/t/how-does-julia-parse-return/123044/3 "2024-11-25T13:45:40Z")

</div>

> [@mrufsvold](#):
>
> `\n` does terminate an expression

If it is complete.

Example:

```julia
x = 1 +
    2

```

Expression is not complete at the first newline but at the second. `x` is assigned 3.

```julia
x = 1
    + 2

```

Complete expressions at both newlines. `x` is assigned 1 and then the expression `+ 2` (unary `+` operation) is evaluated and not assigned to anything.

---

<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: [November 25, 2024, 1:47pm UTC](https://discourse.julialang.org/t/how-does-julia-parse-return/123044/4 "2024-11-25T13:47:57Z")

</div>

You can also ask the [parser](https://github.com/JuliaLang/JuliaSyntax.jl) which output is perhaps a bit more clearn compared to `dump`:

```julia-repl
julia> using JuliaSyntax

julia> str = """
       function example()
           return 1 + 1
       end
       """;

julia> tree = JuliaSyntax.parseall(JuliaSyntax.GreenNode, str)
     1:40 │[toplevel]
     1:39 │ [function]
     1:8 │ function
     9:9 │ Whitespace
    10:18 │ [call]
    10:16 │ Identifier ✔
    17:17 │ (
    18:18 │ )
    19:36 │ [block]
    19:23 │ NewlineWs
    24:35 │ [return]
    24:29 │ return
    30:35 │ [call]
    30:30 │ Whitespace
    31:31 │ Integer ✔
    32:32 │ Whitespace
    33:33 │ + ✔
    34:34 │ Whitespace
    35:35 │ Integer ✔
    36:36 │ NewlineWs
    37:39 │ end
    40:40 │ NewlineWs

julia> show(stdout, MIME"text/plain"(), tree, str)
     1:40 │[toplevel]
     1:39 │ [function]
     1:8 │ function "function"
     9:9 │ Whitespace " "
    10:18 │ [call]
    10:16 │ Identifier ✔ "example"
    17:17 │ ( "("
    18:18 │ ) ")"
    19:36 │ [block]
    19:23 │ NewlineWs "\n "
    24:35 │ [return]
    24:29 │ return "return"
    30:35 │ [call]
    30:30 │ Whitespace " "
    31:31 │ Integer ✔ "1"
    32:32 │ Whitespace " "
    33:33 │ + ✔ "+"
    34:34 │ Whitespace " "
    35:35 │ Integer ✔ "1"
    36:36 │ NewlineWs "\n"
    37:39 │ end "end"
    40:40 │ NewlineWs "\n"

```

---

<div class="post-metadata">

### Author: ![world-peace](https://avatars.discourse-cdn.com/v4/letter/w/9f8e36/32.png) [@world-peace](https://discourse.julialang.org/u/world-peace)
#### Post date: [November 25, 2024, 3:09pm UTC](https://discourse.julialang.org/t/how-does-julia-parse-return/123044/5 "2024-11-25T15:09:45Z")

</div>

> [@GunnarFarneback](#):
>
> If it is complete.

In general, how would one know if an expression is complete or not complete.

There seem to be obvious or intuitive examples, for example the example given of

```julia
x = 1 + 
      2

```

it seems obvious this is not complete, because `+` must be the binary `+` operator here, and there is nothing on the RHS.

However in some cases, such as the cases from my OP, where is less obvious.

Is the way to do it using `JuliaSyntax.parseall`? Or are there other methods/rules which can be used to figure this out?

---

<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: [November 25, 2024, 3:27pm UTC](https://discourse.julialang.org/t/how-does-julia-parse-return/123044/6 "2024-11-25T15:27:03Z")

</div>

> [@world-peace](#):
>
> However in some cases, such as the cases from my OP, where is less obvious.

FWIW, all cases in your OP are obvious to me but that probably depends on how much programming (or julia) experience you have.

Adding a linter error for unreachable code (e.g. any code after `return`) would be a nice addition if anyone is up for contributing to the language server.

---

<div class="post-metadata">

### Author: ![hexaeder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hexaeder/32/24403_2.png) [@hexaeder](https://discourse.julialang.org/u/hexaeder)
#### Post date: [November 25, 2024, 3:42pm UTC](https://discourse.julialang.org/t/how-does-julia-parse-return/123044/7 "2024-11-25T15:42:08Z")

</div>

I think the only odd one out is the implicit return of nothing. Without that special case, `return` behaves just like a variable declaration and you could always replace

```julia
return $expr

```

with

```julia
retval = $expr
return retval

```

The examples in the original post all make perfect sense once you internalize that in Julia, every expression (including control flow) returns “something” (which might be `nothing`) and expressions like

```julia
x = if a<0
    :smaller
else
    :bigger
end

```

or

```julia
x = a == 1 && otherFunction(4)

```

are quite idiomatic.
