# How do method definitions work inside functions?

**URL:** <https://discourse.julialang.org/t/how-do-method-definitions-work-inside-functions/62055>\
**Category:** General Usage\
**Created:** [May 29, 2021, 6:58pm UTC](https://discourse.julialang.org/t/how-do-method-definitions-work-inside-functions/62055 "2021-05-29T18:58:00Z")\
**Posts on this page:** 3\
**Page:** 1

<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, 6:58pm UTC](https://discourse.julialang.org/t/how-do-method-definitions-work-inside-functions/62055/1 "2021-05-29T18:58:00Z")

</div>

I’m trying to understand how method definitions work inside functions. (Is this mentioned anywhere in the documentation?) From [this post](https://discourse.julialang.org/t/if-b-a-when-do-changes-in-a-affect-b/61828/16) I learned that all method definitions for a given local function `f` are combined to an anonymous functions, and each definition is replaced by assigning this anonymous function to `f`. It doesn’t matter where the method definitions occur inside the enclosing function. For example, the function

```julia
function a()
    f() = 0
    return f
    f(x) = x
end

```

returns a function with _two_ methods. If there are two methods with the same signature, then the first one is ignored, and the user gets a warning.

However, there are cases where this explanation doesn’t seem to work (with Julia 1.6.1). For instance, I get

```julia
function b1()
    if true
        f() = 0
    else
        f(x) = x
    end
end

b1() # generic function with 2 methods (as expected)

```

but

```julia
function b2()
    if false
        f() = 0
    else
        f(x) = x
    end
end

b2() # UndefVarError: f not defined

```

The picture gets even confusing if one adds empty functions. Here are some examples:

```julia
function c1()
    function f end
    return f
end

c1() # generic function with 0 methods (as expected)

function c2()
    function f end
    return f
    f() = 0
end

c2() # UndefVarError: f not defined

function c3()
    g = function f end
    return g
end

c3() # generic function with 0 methods (as expected)

function c4()
    g = function f end
    return g
    f() = 0
end

c4() # nothing (which is not even a function)

function c5()
    if false
        f() = 0
    else
        function f end
    end
end

c5() # nothing

```

Is this the intended behavior? If yes, what are the rules behind this?

---

<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 30, 2021, 3:51pm UTC](https://discourse.julialang.org/t/how-do-method-definitions-work-inside-functions/62055/2 "2021-05-30T15:51:04Z")

</div>

See the following issue and a lot of issues that mention it:

[https://github.com/JuliaLang/julia/issues/15602](https://github.com/JuliaLang/julia/issues/15602)

TL;DR: you should not define local methods in different branches, but not all of them error or give an easy to interpret error message.

---

<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 30, 2021, 4:07pm UTC](https://discourse.julialang.org/t/how-do-method-definitions-work-inside-functions/62055/3 "2021-05-30T16:07:46Z")

</div>

[https://github.com/JuliaLang/julia/pull/41013](https://github.com/JuliaLang/julia/pull/41013)
