# Nested function logic

**URL:** <https://discourse.julialang.org/t/nested-function-logic/11251>\
**Category:** General Usage\
**Tags:** question\
**Created:** [May 29, 2018, 8:43pm UTC](https://discourse.julialang.org/t/nested-function-logic/11251 "2018-05-29T20:43:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 29, 2018, 8:43pm UTC](https://discourse.julialang.org/t/nested-function-logic/11251/1 "2018-05-29T20:43:36Z")

</div>

Suppose I define the function `f(x)` as follows:

```julia
function f(x)
       if true
           g(x) = 2x
       else
           g(x) = 3x
       end
       g(x)
end

```

To me it was very surprising that calling `f(2)` returns 6 instead of 2. Can someone explain this behavior?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 29, 2018, 8:52pm UTC](https://discourse.julialang.org/t/nested-function-logic/11251/2 "2018-05-29T20:52:39Z")

</div>

Try

```julia
function f(x)
    if true
        g = x -> 2x
    else
        g = x -> 3x
    end 
    g(x)
end 

```

My understanding is that doing either `function f end` or `f(args...) = #code` creates a function `f` in the namespace of the current module. However anonymous functions (“lambdas”) are not like this.

Note that you could do

```julia
@static if true
    g(x) = 2x
else
    g(x) = 3x
end

```

in the global scope of the module and it should work fine (in fact, you probably don’t even need the `@static` if you do it in global scope).

Ideally I think your code should throw a warning.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 29, 2018, 8:52pm UTC](https://discourse.julialang.org/t/nested-function-logic/11251/3 "2018-05-29T20:52:48Z")

</div>

Related:

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

There should really be some documentation about this. It was totally unexpected until it was explained to me.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 29, 2018, 8:54pm UTC](https://discourse.julialang.org/t/nested-function-logic/11251/4 "2018-05-29T20:54:23Z")

</div>

It doesn’t throw a warning when I execute it at the REPL. Inside a script I do get warnings.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 29, 2018, 8:55pm UTC](https://discourse.julialang.org/t/nested-function-logic/11251/5 "2018-05-29T20:55:20Z")

</div>

That makes sense. I know that the REPL does various things to cut down on compile time (or perhaps just to silence warnings?), but I don’t know the details.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [May 30, 2018, 3:31am UTC](https://discourse.julialang.org/t/nested-function-logic/11251/6 "2018-05-30T03:31:54Z")

</div>

> [@ExpandingMan](#):
>
> Try
> 
> ```julia
> function f(x)
> if true
> g = x -> 2x
> else
> g = x -> 3x
> end 
> g(x)
> end 
> 
> ```

Does this allow you to do multi-method functions?

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [May 30, 2018, 7:44am UTC](https://discourse.julialang.org/t/nested-function-logic/11251/7 "2018-05-30T07:44:56Z")

</div>

Yes it does. Although I’m not 100% sure I understand why this works and my original code doesn’t.
