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

<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).

---

_[View the full topic](https://discourse.julialang.org/t/all-ways-to-define-functions-in-julia/45816)._
