# Weird behaviour with function variables leaking out of if blocks

**URL:** <https://discourse.julialang.org/t/weird-behaviour-with-function-variables-leaking-out-of-if-blocks/72651>\
**Category:** New to Julia\
**Tags:** question, scope, function\
**Created:** [December 6, 2021, 12:32pm UTC](https://discourse.julialang.org/t/weird-behaviour-with-function-variables-leaking-out-of-if-blocks/72651 "2021-12-06T12:32:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)\
**Post date:** [December 6, 2021, 12:32pm UTC](https://discourse.julialang.org/t/weird-behaviour-with-function-variables-leaking-out-of-if-blocks/72651/1 "2021-12-06T12:32:31Z")

</div>

According to the docs, [if blocks are leaky](https://docs.julialang.org/en/v1/manual/control-flow/#man-conditional-evaluation). I can use it to my advantage with the following function `f`, but not with `g`, why?

```julia
@enum Case A B

f = function (case, x)
    if case == A
        coeff = 3
    elseif case == B
        coeff = 8
    end
    coeff * x
end

g = function (case, x)
    if case == A
        lambda(x) = 2x
    elseif case == B
        lambda(x) = x^2
    end
    lambda(x)
end

f(A, 5) # 15
f(B, 5) # 40

g(A, 5) # 25 .. wait, what?
g(B, 5) # ERROR: lambda not defined. 

```

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 6, 2021, 12:36pm UTC](https://discourse.julialang.org/t/weird-behaviour-with-function-variables-leaking-out-of-if-blocks/72651/2 "2021-12-06T12:36:31Z")

</div>

> [@Same code in if and else blocks, get UndefVarError only on else condition](https://discourse.julialang.org/t/same-code-in-if-and-else-blocks-get-undefvarerror-only-on-else-condition/11611):
>
> Suppose I have a function with 2 arguments, and I want to make a new function with one of the arguments fixed to a value, depending on some condition. For example: fun(x,a) = a\*x function foo(flag::Bool) if flag bar(x) = fun(x,2) else bar(x) = fun(x,2) end return bar end In this example the value of a is fixed to 2 on both branches, just for illustration. If I run this code in a script, foo(true) returns the desired function, but foo(false) gives ERROR: UndefVarErr…

---

<div class="post-metadata">

**Author:** ![iago-lito](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iago-lito/32/31924_2.png) [@iago-lito](https://discourse.julialang.org/u/iago-lito)\
**Post date:** [December 6, 2021, 12:44pm UTC](https://discourse.julialang.org/t/weird-behaviour-with-function-variables-leaking-out-of-if-blocks/72651/3 "2021-12-06T12:44:42Z")

</div>

Note that `g(A, 5)` passed with no warning and a completely undesired behaviour :\ Is there anything I can turn on to avoid this being silent?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 6, 2021, 1:01pm UTC](https://discourse.julialang.org/t/weird-behaviour-with-function-variables-leaking-out-of-if-blocks/72651/4 "2021-12-06T13:01:47Z")

</div>

we should definitely make this a parser error.
