# Defining functions using begin

**URL:** <https://discourse.julialang.org/t/defining-functions-using-begin/37090>\
**Category:** New to Julia\
**Created:** [April 6, 2020, 5:26am UTC](https://discourse.julialang.org/t/defining-functions-using-begin/37090 "2020-04-06T05:26:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![risingganymede](https://avatars.discourse-cdn.com/v4/letter/r/e495f1/32.png) [@risingganymede](https://discourse.julialang.org/u/risingganymede)\
**Post date:** [April 6, 2020, 5:26am UTC](https://discourse.julialang.org/t/defining-functions-using-begin/37090/1 "2020-04-06T05:26:42Z")

</div>

I found that it is possible to define functions like this:

```julia
f(x, y) = begin
    x + y
end

```

I couldn’t find documentation for this style of function definition in the Julia manual. My question is thus: what differences does this style have over defining functions using the more oft-used `function` blocks.

---

<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:** [April 6, 2020, 6:22am UTC](https://discourse.julialang.org/t/defining-functions-using-begin/37090/2 "2020-04-06T06:22:27Z")

</div>

Most people would use

```julia
f(x, y) = x + y

```

since function definitions already introduce a block (so `begin` here is redundant, but harmless).

See the docs at [`?begin`](https://docs.julialang.org/en/v1/base/base/#begin).

---

<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:** [April 6, 2020, 9:49am UTC](https://discourse.julialang.org/t/defining-functions-using-begin/37090/3 "2020-04-06T09:49:02Z")

</div>

It’s a bit of a sneaky ‘abuse’ of short form (or ‘assignment form’) function definition. You can find it documented here: [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/)

While it doesn’t explicitly mention using `begin ... end`, it says

> In the assignment form, the body of the function must be a single expression, although it can be a compound expression (see [Compound Expressions](https://docs.julialang.org/en/v1/manual/control-flow/#man-compound-expressions-1)). Short, simple function definitions are common in Julia. The short function syntax is accordingly quite idiomatic, considerably reducing both typing and visual noise.

Compound expressions can entail `begin ... end`.

While the manual says that that this form is idiomatic, I believe this is the case only for very short function bodies, typically a single line. Most of the style guides I’ve seen recommend using short form only for single line functions.

I guess some people use the short form anyway also for longer functions, because it reminds them of syntax in languages they are familiar with (I think R, in particular).

Personally, I would recommend to use short form only for single-line function definitions, I find the `begin ... end` trick less readable, and it also clashes with coding practices in almost all Julia code, such as Base, stdlib and most registered packages.

---

<div class="post-metadata">

**Author:** ![tkluck](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkluck/32/15769_2.png) [@tkluck](https://discourse.julialang.org/u/tkluck)\
**Post date:** [April 6, 2020, 11:33am UTC](https://discourse.julialang.org/t/defining-functions-using-begin/37090/4 "2020-04-06T11:33:49Z")

</div>

I’ve recently started really liking the

```julia
f(x) = begin
    ...
end

```

form, mostly because it works really well in a list of methods where many are one-liners but a few are longer. As a short [example from `HAML.jl`](https://github.com/tkluck/HAML.jl/blob/master/src/Hygiene.jl):

```julia
mapexpr(f, expr) = expr
mapexpr(f, expr::Expr) = begin
    res = Expr(expr.head)
    resize!(res.args, length(expr.args))
    map!(f, res.args, expr.args)
    return res
end

```

I wouldn’t be surprised if I find myself migrating 100% away from the `function ...` syntax, even.

---

<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:** [April 6, 2020, 1:43pm UTC](https://discourse.julialang.org/t/defining-functions-using-begin/37090/5 "2020-04-06T13:43:49Z")

</div>

Since `function` is such an vital and pervasive code block signifier, I would be unhappy if this were to become the common idiom (or even replacing the `function` keyword – I only worry since yours is apparently a popular opinion).

I see how it works well in the limited case that you mention (list of many one-liners with a few longer), but I think it really makes it hard to visually distinguish code blocks when the keyword isn’t at the beginning of the line, and doesn’t line up with the matching `end`.

For the same reason, I find that putting macros like `@inbounds` and `@threads` ahead of a `for` loop messes up the code quite a bit. I would love to be able to write

```julia
@inbounds
for i in eachindex(X)
    ...
end

```
