# 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:** 1

<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:** [August 31, 2020, 6:28am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/1 "2020-08-31T06:28:30Z")

</div>

I tried to list all the ways one can define functions in Julia. Some are practical, some are silly but collecting them all is fun.

Planning to make a video for fun exploring this.

```julia

function name(a, b)
    a + b
end

name(a,b) = a + b

if false
    name = (a,b) -> a+b
end

name2 = (a,b) -> a+b

name3 = function(a, b)
    a+b
end

macro defining_function(fnname)
    :(
        function $fnname(a, b)
            a+b
        end
    )
end

@defining_function name4

function fn_generator()
    function (a, b)
        a*b
    end
end

name5 = fn_generator()

function fn_generator2()
    function name(a, b)
        a*b
    end
end

# what would happen here?
fn_generator2()

```

**edit**

Thanks to @thofma for `a+b=a*b`

I just thought of `LambdaFn.jl` [GitHub - haberdashPI/LambdaFn.jl: Alternative, underscore syntax for anonymous functions in Julia](https://github.com/haberdashPI/LambdaFn.jl) which allows

`@lf _+_`

---

<div class="post-metadata">

**Author:** ![thofma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thofma/32/1691_2.png) [@thofma](https://discourse.julialang.org/u/thofma)\
**Post date:** [August 31, 2020, 6:40am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/2 "2020-08-31T06:40:56Z")

</div>

`a * b = a + b`

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 31, 2020, 6:53am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/3 "2020-08-31T06:53:16Z")

</div>

I am shocked and horrified. 😂

---

<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:** [August 31, 2020, 6:53am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/4 "2020-08-31T06:53:38Z")

</div>

> [@thofma](#):
>
> a \* b = a + b

Mind blown!

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 31, 2020, 6:56am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/5 "2020-08-31T06:56:17Z")

</div>

```julia
name3 = function(a, b)
    a+b
end

```

This one is also surprising to me. Is that described somewhere in the manual? It’s rather reminiscent of the way you define functions in R…

---

<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:** [August 31, 2020, 7:02am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/6 "2020-08-31T07:02:16Z")

</div>

Yeah. It’s got interesting properties though.

---

<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:** [August 31, 2020, 7:06am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/7 "2020-08-31T07:06:08Z")

</div>

the RHS is just another syntax to define an anonymous function so it’s the same as

```julia
name3 = (a,b) -> a+b

```

But `typeof(name3)` is NOT `name`! It’s the “name” given to the anonymous function.

Interestingly, `@edit name3(1,2)` can stil find it

---

<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 28, 2021, 12:03am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/8 "2021-05-28T00:03:20Z")

</div>

This is very interesting as well

```julia
function (x::Int)(y)

    x+y

end

x = 2

x(2)

```

what is this called? `function (x::Int)(y)`

---

<div class="post-metadata">

**Author:** ![tbeason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tbeason/32/15898_2.png) [@tbeason](https://discourse.julialang.org/u/tbeason)\
**Post date:** [May 28, 2021, 12:12am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/9 "2021-05-28T00:12:06Z")

</div>

Functors, typically.

[https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects](https://docs.julialang.org/en/v1/manual/methods/#Function-like-objects)

---

<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 28, 2021, 12:21am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/10 "2021-05-28T00:21:02Z")

</div>

> [@tbeason](#):
>
> Functors, typically.

Can we change that? afaik it’s only C++ that calls those “functors” and everywhere else “functors” are a different thing. “Callables” is a good and widely used name.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [May 28, 2021, 1:03am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/11 "2021-05-28T01:03:47Z")

</div>

```julia
struct Name <: Function end

(::Name)(a,b) = a+b

const name = Name()

```

---

<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 28, 2021, 1:18am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/12 "2021-05-28T01:18:36Z")

</div>

> [@jzr](#):
>
> “functors” and everywhere else

yeah, i got a pure math degree so functor is a totally different thing to me I think.

---

<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 28, 2021, 11:36pm UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/13 "2021-05-28T23:36:25Z")

</div>

> [@xiaodai](#):
>
> ```julia
> if false
> name = (a,b) -> a+b
> end
> 
> ```

What do you mean by that example? If you enter it at the Julia prompt, then `name` is not defined afterwards.

---

<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, 1:25am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/14 "2021-05-29T01:25:31Z")

</div>

Just the content within false

---

<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, 2:39am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/15 "2021-05-29T02:39:42Z")

</div>

What about this one?

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

name = fn_generator()

name(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, 2:47am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/16 "2021-05-29T02:47:49Z")

</div>

cool

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [May 29, 2021, 7:35am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/17 "2021-05-29T07:35:54Z")

</div>

This is the same as

```julia
function fn_generator()
        f(x, y) = x+y
end

```

since `true` branch is ignored. And original version should produce warning in 1.6 or 1.7

---

<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:46am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/18 "2021-05-29T07:46:03Z")

</div>

This is surprising. What is happening here?

---

<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, 7:48am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/19 "2021-05-29T07:48:12Z")

</div>

The last definition of `f` is used. Some may see this as a bug. To avoid this “behaviour” use anonymous functions instead.

```julia
julia> function fn_generator()
           if true
               f = () -> 0
           else
               f = (x, y) -> x+y
           end
       end
fn_generator (generic function with 1 method)

julia> name = fn_generator()
#10 (generic function with 1 method)

julia> name()
0

```

See [disallow methods of local functions in different blocks · Issue #15602 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15602) and [Defining a function inside if...else..end NOT as expected?](https://discourse.julialang.org/t/defining-a-function-inside-if-else-end-not-as-expected/13815).

---

<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, 7:52am UTC](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816/20 "2021-05-29T07:52:07Z")

</div>

> [@Skoffer](#):
>
> And original version should produce warning in 1.6 or 1.7

I don’t see a warning for either version. Can you demonstrate this?

UPDATE:

Ah, I see, the function signature must be the same (it isn’t in the example above):

```julia
julia> function fn_generator()
           if true
               f() = 0
           else
               f() = 2
           end
       end
WARNING: Method definition f() in module Main at REPL[1]:3 overwritten at REPL[1]:5.
fn_generator (generic function with 1 method)

```

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