# Ways of defining a function

**URL:** https://discourse.julialang.org/t/ways-of-defining-a-function/3465
**Category:** General Usage
**Tags:** question
**Created:** [May 1, 2017, 6:09pm UTC](https://discourse.julialang.org/t/ways-of-defining-a-function/3465 "2017-05-01T18:09:05Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [May 1, 2017, 6:09pm UTC](https://discourse.julialang.org/t/ways-of-defining-a-function/3465/1 "2017-05-01T18:09:05Z")

</div>

Most julia functions are defined like this:

```nohighlight
function f(x)
    # some code
end

```

However a few packages use the following style

```nohighlight
f(x) = begin
    # some code
end

```

Is there a semantic difference between the two or are they exactly the same?

---

<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: [May 1, 2017, 6:20pm UTC](https://discourse.julialang.org/t/ways-of-defining-a-function/3465/2 "2017-05-01T18:20:09Z")

</div>

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

```

is just shorthand for

```julia
function f(x)
    ...
end

```

That said, using the shorthand with a `begin ... end` block just defeats its purpose. Use it when your code is short (one-liners).

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [May 1, 2017, 6:28pm UTC](https://discourse.julialang.org/t/ways-of-defining-a-function/3465/3 "2017-05-01T18:28:58Z")

</div>

Ok do I get this right:

- `function f(x) ... end`
- `f(x) = begin ... end`
- `f(x) = let ... end`
- `f(x) = ... `  
are all lowered to exactly the same thing? Analogous for more complicated signatures like `f(x::Int,y::T; kw...) where T`?

---

<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: [May 1, 2017, 7:06pm UTC](https://discourse.julialang.org/t/ways-of-defining-a-function/3465/4 "2017-05-01T19:06:07Z")

</div>

`let` has different [scoping behavior](https://docs.julialang.org/en/latest/manual/variables-and-scoping.html), but other than that, yes. Note that the answer to your question is in the [documentation](https://docs.julialang.org/en/latest/manual/functions.html), so please try to read that.

---

<div class="post-metadata">

### Author: ![fengyang.wang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fengyang.wang/32/104_2.png) [@fengyang.wang](https://discourse.julialang.org/u/fengyang.wang)
#### Post date: [May 1, 2017, 7:50pm UTC](https://discourse.julialang.org/t/ways-of-defining-a-function/3465/5 "2017-05-01T19:50:56Z")

</div>

Some authors may be influenced by the Scala style guide, which suggests that pure functions be written with the Scala equivalent of `f(x) = ...`, and impure functions be written as `function f(x) ... end` regardless of how many lines `...` requires. Since this style is clear and useful, despite not being prevalent, I would personally suggest that it’s up to you whether to use `begin ... end`.

---

<div class="post-metadata">

### Author: ![jw3126](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jw3126/32/3086_2.png) [@jw3126](https://discourse.julialang.org/u/jw3126)
#### Post date: [May 1, 2017, 9:21pm UTC](https://discourse.julialang.org/t/ways-of-defining-a-function/3465/6 "2017-05-01T21:21:46Z")

</div>

Can you give an example, when  
`f(x) = begin ... end`  
`f(x) = let ... end`  
differ?

---

<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: [May 2, 2017, 6:26am UTC](https://discourse.julialang.org/t/ways-of-defining-a-function/3465/7 "2017-05-02T06:26:09Z")

</div>

The function definition already introduces hard scope, so in this case they don’t differ. My point was about `let` in general; I only use it when I want its specific scoping behavior, but in your example it does not make a difference.

---

<div class="post-metadata">

### Author: ![Evizero](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/evizero/32/10118_2.png) [@Evizero](https://discourse.julialang.org/u/Evizero)
#### Post date: [May 2, 2017, 10:14am UTC](https://discourse.julialang.org/t/ways-of-defining-a-function/3465/8 "2017-05-02T10:14:35Z")

</div>

`<unsolicitedopinion>`

Can we please **not** promote this style

```julia
f(x) = begin
    # multiline code
end

```

`</unsolicitedopinion>`

---

<div class="post-metadata">

### Author: ![pint](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pint/32/125_2.png) [@pint](https://discourse.julialang.org/u/pint)
#### Post date: [May 2, 2017, 11:12am UTC](https://discourse.julialang.org/t/ways-of-defining-a-function/3465/9 "2017-05-02T11:12:43Z")

</div>

i do like this style. primarily because

1, i don’t like that there are two syntaxes for the same thing

2, i often find myself changing back and force between a one liner and a few liner, or ? vs if, as the code develops. it is a hassle to restructure.

so i usually just use f() = … exclusively
