# Scope and Inlining

**URL:** <https://discourse.julialang.org/t/scope-and-inlining/62694>\
**Category:** General Usage\
**Tags:** scope\
**Created:** [June 10, 2021, 6:44pm UTC](https://discourse.julialang.org/t/scope-and-inlining/62694 "2021-06-10T18:44:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaximilianJHuber](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maximilianjhuber/32/2579_2.png) [@MaximilianJHuber](https://discourse.julialang.org/u/MaximilianJHuber)\
**Post date:** [June 10, 2021, 6:44pm UTC](https://discourse.julialang.org/t/scope-and-inlining/62694/1 "2021-06-10T18:44:29Z")

</div>

I stumbled across a case of bad practice that resulted in an error, but I am not sure why.

```julia
function g(y)
    function f(x)
        f = x * y
        return f
    end

    f(y)
    f(y)
end

g(0.2)

```

I really should not define a variable `f` within a function `f`. The about code throws `ERROR: LoadError: MethodError: objects of type Float64 are not callable` and points to the second call of `f` within `g`.

At that time, f is a `Float64` but why? Does that have to do with inlining? Calling `f` once instead of twice within `g` works out fine.

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 10, 2021, 6:51pm UTC](https://discourse.julialang.org/t/scope-and-inlining/62694/2 "2021-06-10T18:51:56Z")

</div>

```julia
julia> f = 1.0
1.0

julia> x = 2.0
2.0

julia> f(x)
ERROR: MethodError: objects of type Float64 are not callable

```

when you do `f(y)`, `f` becomes the result of `f = x*y` which happens to be a Float64. And you are then trying to “call” that float with parameter `y`.

```julia
julia> function g(y)
           function f(x)
               f = x * y
               return f
           end
           println(f," ",typeof(f))
           f(y)
           println(f," ",typeof(f))
           f(y)
       end
g (generic function with 1 method)

julia> g(2)
f var"#f#5"{Int64}
4 Int64

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 10, 2021, 7:04pm UTC](https://discourse.julialang.org/t/scope-and-inlining/62694/3 "2021-06-10T19:04:21Z")

</div>

This has nothing to do with inlining and is entirely due to how local scope works. Function `g` introduces a local scope. It has a local variable `f`, which is initially bound to a function. Now inside that function, you _also_ assign to `f`. Because this is all inside a local scope, that inner `f` assignment doesn’t shadow the outer `f` function (like it would if this inner function were defined at global scope) and instead it is the same binding. Thus a side-effect of executing that inner function is _changing what `f` refers to_.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [June 10, 2021, 7:15pm UTC](https://discourse.julialang.org/t/scope-and-inlining/62694/4 "2021-06-10T19:15:28Z")

</div>

A simpler example of the same rules in action would be:

```julia
julia> let z = 0
           let
               z = 1
           end
           z
       end
1

```

versus

```julia
julia> let
           let
               z = 1
           end
           z
       end
ERROR: UndefVarError: z not defined
Stacktrace:
 [1] top-level scope
   @ REPL[25]:5

```
