# Return value of \`begin ... end\`

**URL:** <https://discourse.julialang.org/t/return-value-of-begin-end/127010>\
**Category:** New to Julia\
**Tags:** question, syntax\
**Created:** [March 16, 2025, 1:53pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010 "2025-03-16T13:53:42Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![LEXUGE](https://avatars.discourse-cdn.com/v4/letter/l/53a042/32.png) [@LEXUGE](https://discourse.julialang.org/u/LEXUGE)\
**Post date:** [March 16, 2025, 1:53pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/1 "2025-03-16T13:53:42Z")

</div>

I understand the expression will be evaluated to a value. So

```julia
a = 1

```

will be evaluated to 1 where as `a=1;` will not

However, I expect

```julia
begin a = 1; end

```

to evaluate to nothing as well. Why does it evaluate to 1? Intuitively I would expect the value of `begin ... end` expression be value of the last expression in the block.

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 16, 2025, 2:03pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/2 "2025-03-16T14:03:07Z")

</div>

> [@LEXUGE](#):
>
> where as `a=1;` will not

The `;` just supresses showing the value in the REPL, it doesn’t affect evaluation:

```julia-repl
julia> (a = 1;)
1

```

> [@LEXUGE](#):
>
> ```julia
> begin a = 1; end
> 
> ```

That’s the same thing as `begin a = 1 end`. Actually, `begin ... end` is not much different that just using parentheses, except in readability. These are the same:

```julia
begin
    f()
    g()
end

```

```julia
begin f(); g() end

```

```julia
(
    f();
    g()
)

```

```julia
(f(); g())

```

---

<div class="post-metadata">

**Author:** ![LEXUGE](https://avatars.discourse-cdn.com/v4/letter/l/53a042/32.png) [@LEXUGE](https://discourse.julialang.org/u/LEXUGE)\
**Post date:** [March 16, 2025, 2:11pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/3 "2025-03-16T14:11:21Z")

</div>

If `;` only suppresses value, does that mean

```julia
b = (a = 1);

```

and

```julia
b = (a = 1;)

```

are the same?

If so, may I also ask what the reasoning behind this design is?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [March 16, 2025, 2:26pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/4 "2025-03-16T14:26:39Z")

</div>

Yeah. The trailing semicolon at the end of an expression doesn’t affect semantics (except in different contexts, such as in a call with keyword arguments). It serves merely to supress display in the REPL, nothing to do with evaluation semantics.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 16, 2025, 5:06pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/5 "2025-03-16T17:06:28Z")

</div>

> [@LEXUGE](#):
>
> If so, may I also ask what the reasoning behind this design is?

I guess I would ask the opposite: why wouldn’t it work this way?

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 16, 2025, 6:47pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/6 "2025-03-16T18:47:38Z")

</div>

I guess, to answer my own question, there are a few “theories of semicolons” you could have. We’re all on the same page that semicolons can be used to separate multiple expressions on the same line (instead of a newline) or optionally terminate complete expressions before the newline.

One theory is that they don’t change the meaning or behavior of the surrounding expressions or insert any invisible expressions. This is the way Julia works. There’s a heuristic in the REPL that if the input expression ends with a semicolon, we suppress printing, but that’s just a heuristic, the behavior of expressions doesn’t change.

Another theory is the “Matlab theory”, which is that statements (Julia doesn’t really have statements, so this would already start to strain the theory) are implicitly surrounded by a print call and putting a semicolon at the end either removes that print call, or maybe causes the statement to evaluate to a value like Julia’s `nothing` that doesn’t output anything when printed. Of course, Julia doesn’t have this—things are not printed by default.

This question seems to be coming from an intermediate theory where a semicolon at the end of an expression implicitly inserts `nothing` after the semicolon, turning an expression that would evaluate to some value into a chain that evaluates to `nothing` instead. If we wanted to bake the “semicolon to suppress printing” thing into the language deeper than being a REPL feature, we could have done this. However, that’s not how it works.

---

<div class="post-metadata">

**Author:** ![LEXUGE](https://avatars.discourse-cdn.com/v4/letter/l/53a042/32.png) [@LEXUGE](https://discourse.julialang.org/u/LEXUGE)\
**Post date:** [March 17, 2025, 11:00am UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/7 "2025-03-17T11:00:46Z")

</div>

I think a common “misfeature” to this design would be

```julia
julia> if (a = true) print("true!") else print("false!") end
true!

```

Such “typo” would be errored in language like Rust where `a=true` evaluates to `()` and cannot be used as a condition.

I feel this weird because I am more mentally inclined to Rust’s design where adding an `;` to the end of expression makes the expression evaluate to `()`, so it’s more than a surgar at REPL level

---

<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:** [March 17, 2025, 11:37am UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/8 "2025-03-17T11:37:17Z")

</div>

> [@LEXUGE](#):
>
> I think a common “misfeature” to this design would be
> 
> `julia> if (a = true) `

This is not very common in practice, I think. If you want to test whether a variable is `true`, you would normally do simply `if a` rather than `if a == true`, so there is less danger of accidentally typing `if a = true`. I’ve never seen the latter mistake in real code.

(Contrast this with C, where almost anything can be used as a conditional value, and accidental assignments in branches are such a common error that most compilers have a warning for this.)

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 17, 2025, 12:13pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/9 "2025-03-17T12:13:33Z")

</div>

There is no semicolon here, so how would that help?

---

<div class="post-metadata">

**Author:** ![LEXUGE](https://avatars.discourse-cdn.com/v4/letter/l/53a042/32.png) [@LEXUGE](https://discourse.julialang.org/u/LEXUGE)\
**Post date:** [March 17, 2025, 12:19pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/10 "2025-03-17T12:19:06Z")

</div>

Yeah, it’s a separate issue where the assignment returns the value assigned instead of `nothing`. But here even with semicolons, one couldn’t suppress this side-effect.

---

<div class="post-metadata">

**Author:** ![LEXUGE](https://avatars.discourse-cdn.com/v4/letter/l/53a042/32.png) [@LEXUGE](https://discourse.julialang.org/u/LEXUGE)\
**Post date:** [March 17, 2025, 12:20pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/11 "2025-03-17T12:20:46Z")

</div>

I agree it’s not very common and probably easy to spot. I am just in general not very satisfied with the side-effect on these expression evaluation (and not able to disable them with `;`)

Sometimes such design is useful as in `a = b = 1`.

---

<div class="post-metadata">

**Author:** ![LEXUGE](https://avatars.discourse-cdn.com/v4/letter/l/53a042/32.png) [@LEXUGE](https://discourse.julialang.org/u/LEXUGE)\
**Post date:** [March 17, 2025, 12:27pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/12 "2025-03-17T12:27:09Z")

</div>

But anyway, I think my original question has been answered. Thanks guys!

---

<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:** [March 17, 2025, 12:44pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/13 "2025-03-17T12:44:25Z")

</div>

> [@LEXUGE](#):
>
> I am just in general not very satisfied with the side-effect on these expression evaluation

An expression having a value is not a [side effect](https://en.wikipedia.org/wiki/Side_effect_(computer_science)).

---

<div class="post-metadata">

**Author:** ![LEXUGE](https://avatars.discourse-cdn.com/v4/letter/l/53a042/32.png) [@LEXUGE](https://discourse.julialang.org/u/LEXUGE)\
**Post date:** [March 17, 2025, 12:58pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/14 "2025-03-17T12:58:59Z")

</div>

Sorry for calling the name wrong.

I think my point is:

1. In Julia the assignment is an expression instead of a statement.
2. I cannot make assignment into a statement through `;` or `begin end`.

And I believe sometimes ensuring assignment is a statement is worthwhile (more context [language agnostic - What is the benefit of having the assignment operator return a value? - Software Engineering Stack Exchange](https://softwareengineering.stackexchange.com/questions/228851/what-is-the-benefit-of-having-the-assignment-operator-return-a-value?newreg=101219557f1645069269a9c23494302b))

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [March 17, 2025, 2:09pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/15 "2025-03-17T14:09:38Z")

</div>

Julia, like Lisp (and most functional programming languages), is an [expression-oriented language](https://en.wikipedia.org/wiki/Expression-oriented_programming_language) and doesn’t have statements. You’ll note that in Python, assignment is a statement. But then they eventually felt compelled to add the `:=` operator just so that you can do an assignment that is an expression. Of course, why have assignment be a statement in the first place? So that you can’t accidentally do `if x = 123` and have it evaluate to true. But Julia doesn’t have truthiness, which is a [fundamentally broken concept](https://discourse.julialang.org/t/on-the-arbitrariness-of-truth-iness/81381), so this isn’t a big issue in the first place.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [March 17, 2025, 3:45pm UTC](https://discourse.julialang.org/t/return-value-of-begin-end/127010/16 "2025-03-17T15:45:43Z")

</div>

A few years ago I wrote a blog post about the return value of assignment and consideration of possible problems in conditionals (coming to similar conclusions as above): [Returned value of assignment in Julia](https://giordano.github.io/blog/2017-12-02-julia-assignment/)
