# All ways to define functions in Julia?

**URL:** https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816
**Category:** General Usage
**Tags:** functions
**Created:** [August 31, 2020, 6:28am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816 "2020-08-31T06:28:30Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [May 29, 2021, 8:01am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/21 "2021-05-29T08:01:25Z")

</div>

There’s another way:

```julia
julia> name4 = (a + b for a in 1:2, b in 3:4).f
#1 (generic function with 1 method)

julia> name4((1, 2))
3

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [May 29, 2021, 8:39am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/22 "2021-05-29T08:39:28Z")

</div>

Mind blowing!!!

---

<div class="post-metadata">

### Author: ![carstenbauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/carstenbauer/32/4981_2.png) [@carstenbauer](https://discourse.julialang.org/u/carstenbauer)
#### Post date: [May 29, 2021, 8:40am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/23 "2021-05-29T08:40:30Z")

</div>

Agreed. But I hope that no one will ever use this 😃

---

<div class="post-metadata">

### Author: ![matthias314](https://avatars.discourse-cdn.com/v4/letter/m/a88e4f/32.png) [@matthias314](https://discourse.julialang.org/u/matthias314)
#### Post date: [May 29, 2021, 12:26pm UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/24 "2021-05-29T12:26:30Z")

</div>

> [@carstenbauer](#):
>
> The last definition of `f` is used.

I don’t think so. In my example,

```julia
function fn_generator()
    if true
        f() = 0
    else
        f(x, y) = x+y
    end
end

```

both method definitions are returned:

```julia
julia> fn_generator()
(::var"#f#1") (generic function with 2 methods)

```

However, if instead I say

```julia
function fn_generator2()
    if false
        f(x, y) = x+y
    else
        f() = 0
    end
end

```

then I get an error:

```julia
julia> fn_generator2()
ERROR: UndefVarError: f not defined

```

Is that intended?

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [May 29, 2021, 2:42pm UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/25 "2021-05-29T14:42:07Z")

</div>

The simplest one: `function foo end`

---

<div class="post-metadata">

### Author: ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)
#### Post date: [May 29, 2021, 5:34pm UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/26 "2021-05-29T17:34:12Z")

</div>

More brackets:  
`(Δ = ((((((((()))))((((((())))))) = ())))))`  
`Δ(2)`

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [May 29, 2021, 7:33pm UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/27 "2021-05-29T19:33:31Z")

</div>

Does that work for you? I get an error.

```julia
julia> (Δ = ((((((((()))))((((((())))))) = ())))))
ERROR: syntax: invalid function name "()" around REPL[3]:100: 

```

---

<div class="post-metadata">

### Author: ![Wei\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_yang/32/23951_2.png) [@Wei\_Yang](https://discourse.julialang.org/u/Wei_Yang)
#### Post date: [May 29, 2021, 7:38pm UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/28 "2021-05-29T19:38:05Z")

</div>

```julia
julia> (Δ = ((((((((()))))((((((())))))) = ())))))
#s1 (generic function with 1 method)

julia> Δ(2)
()

```

This is what I got.

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [May 29, 2021, 8:05pm UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/29 "2021-05-29T20:05:33Z")

</div>

Looks like it’s new in 1.6. A smaller version is

```julia
julia> (()() = ())()
()

```

---

<div class="post-metadata">

### Author: ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)
#### Post date: [May 30, 2021, 4:19am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/30 "2021-05-30T04:19:36Z")

</div>

This recalls:

```julia
julia> where where where where where where where where where
Any

```

😅

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [May 30, 2021, 4:32am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/31 "2021-05-30T04:32:31Z")

</div>

```julia
julia> where where where
Any

```

why does this work?

---

<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 31, 2021, 9:36am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/32 "2021-05-31T09:36:33Z")

</div>

Same as `T where T`, with a variable called `where`.

---

<div class="post-metadata">

### Author: ![waldyrious](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/waldyrious/32/80_2.png) [@waldyrious](https://discourse.julialang.org/u/waldyrious)
#### Post date: [June 1, 2021, 11:25am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/33 "2021-06-01T11:25:22Z")

</div>

Huh, interesting. I would have expected `where` to be a reserved word in Julia, or at least throw some kind of warning. On the other hand, one would have to go deliberately out of their way to make that construct, so forbidding it may arguably not be warranted.

---

<div class="post-metadata">

### Author: ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)
#### Post date: [June 1, 2021, 12:45pm UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/34 "2021-06-01T12:45:45Z")

</div>

wow! Why can I do this:

```julia
julia> a*b=a+b
* (generic function with 1 method)

julia> 2*3
5

```

but not this

```julia
julia> 2*3
6

julia> a*b=a+b
ERROR: error in method definition: function Base.* must be explicitly imported to be extended
Stacktrace:
 [1] top-level scope
   @ none:0
 [2] top-level scope
   @ REPL[1]:1

```

after starting a new REPL each time? Shouldn’t the first one give the same warning?

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [June 1, 2021, 12:51pm UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/35 "2021-06-01T12:51:57Z")

</div>

> [@fgerick](#):
>
> Shouldn’t the first one give the same warning?

I agree that that in particular could show a warning or an error. But note that this is the behaviour for every function exported by any module:

```julia
julia> module A
         export f
         f(x) = 1
       end
Main.A

julia> using .A

julia> f(x) = 2
f (generic function with 1 method)

julia> f(1)
2

```

while:

```julia
julia> module A
         export f
         f(x) = 1
       end
Main.A

julia> using .A

julia> f(1)
1

julia> f(x) = 2
ERROR: error in method definition: function A.f must be explicitly imported to be extended

```

It would be really annoying if we could not define the function if the name we wanted because some package exported that name. The behaviour is that if you define your own function first, then you take that name, if you use the package’s function first, then if you try to redefine it, you must explicitly extend it.

---

<div class="post-metadata">

### Author: ![fgerick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fgerick/32/13228_2.png) [@fgerick](https://discourse.julialang.org/u/fgerick)
#### Post date: [June 1, 2021, 1:00pm UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/36 "2021-06-01T13:00:28Z")

</div>

Thanks for clarifying, up until now I assumed that your first example would also give an error! That might have caused some confusion in the past 😅

---

<div class="post-metadata">

### Author: ![Pangoraw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pangoraw/32/24719_2.png) [@Pangoraw](https://discourse.julialang.org/u/Pangoraw)
#### Post date: [June 2, 2021, 7:14am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/37 "2021-06-02T07:14:47Z")

</div>

Another one, I have not seen yet

```julia
julia> name1 = begin
           x
           y=2
       end -> x + y
#1 (generic function with 1 method)

julia> name1(1)
3

julia> name1(2;y=2)
4

julia> name2 = begin
           x
           y
       end -> x + y
#4 (generic function with 1 method)

julia> name2(2; y=2)
4

```

---

<div class="post-metadata">

### Author: ![ettersi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ettersi/32/6829_2.png) [@ettersi](https://discourse.julialang.org/u/ettersi)
#### Post date: [June 2, 2021, 8:57am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/38 "2021-06-02T08:57:09Z")

</div>

Why is `begin; x; y; end` equivalent to `(x;y)` in this context?

---

<div class="post-metadata">

### Author: ![Pangoraw](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pangoraw/32/24719_2.png) [@Pangoraw](https://discourse.julialang.org/u/Pangoraw)
#### Post date: [June 2, 2021, 9:37am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/39 "2021-06-02T09:37:18Z")

</div>

I am not too sure but I would say that multiple unrelated expressions are parsed as a block so `(x;y)` is a valid block. You can then replace `(x;y)` by `begin; x; y; end` in the function declaration.

```julia
julia> :((x;y)) |> dump
Expr
  head: Symbol block
  args: Array{Any}((3,))
    1: Symbol x
    2: LineNumberNode
      line: Int64 1
      file: Symbol REPL[1]
    3: Symbol y

```

---

<div class="post-metadata">

### Author: ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)
#### Post date: [June 2, 2021, 10:24am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/40 "2021-06-02T10:24:28Z")

</div>

this thread is getting better and better

[Previous page](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816.md?page=1)

[Next page](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816.md?page=3)
