# Define function with begin or with let?

**URL:** https://discourse.julialang.org/t/define-function-with-begin-or-with-let/44767
**Category:** General Usage
**Created:** [August 11, 2020, 10:36pm UTC](https://discourse.julialang.org/t/define-function-with-begin-or-with-let/44767 "2020-08-11T22:36:38Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [August 11, 2020, 10:36pm UTC](https://discourse.julialang.org/t/define-function-with-begin-or-with-let/44767/1 "2020-08-11T22:36:38Z")

</div>

Consider these two functions

```julia
f(x) = begin
    x^2
end

g(x) = let
    x^2
end

using InteractiveUtils

@code_warntype f(1)
@code_warntype g(1)

```

The compiler seems to generate the same code for both functions. So my question is: is there any difference between these two?

---

<div class="post-metadata">

### Author: ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)
#### Post date: [August 11, 2020, 10:54pm UTC](https://discourse.julialang.org/t/define-function-with-begin-or-with-let/44767/2 "2020-08-11T22:54:43Z")

</div>

My question is why you would define your function this way instead of:

```julia
function f(x)
    x^2
end

```

You are basically using the single-line syntax to define a multi-line function using a block to avoid the function scope being ended by the first newline found. If your intent is saving some keystrokes you could as well use parenthesis instead of `begin ... end` or `let ... end`.

```julia
f(x) = (
    x^2
)

```

Probably there is no difference between the two forms, unless you actually use the `let` feature of allocating new bindings (by defining them in the same line as the `let` keyword).

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [August 11, 2020, 10:59pm UTC](https://discourse.julialang.org/t/define-function-with-begin-or-with-let/44767/3 "2020-08-11T22:59:09Z")

</div>

Also, the compiler doesn’t care a little bit about syntax, the only thing matter is the scope. As long as the two version has identical scope behavior, which they do in this case and a slight generalization of this, they’ll be have the same.
