# Functions inside functions using "global" variables

**URL:** <https://discourse.julialang.org/t/functions-inside-functions-using-global-variables/31434>\
**Category:** Performance\
**Created:** [November 23, 2019, 10:22pm UTC](https://discourse.julialang.org/t/functions-inside-functions-using-global-variables/31434 "2019-11-23T22:22:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [November 23, 2019, 10:22pm UTC](https://discourse.julialang.org/t/functions-inside-functions-using-global-variables/31434/1 "2019-11-23T22:22:59Z")

</div>

Hi!

I need help about constructing functions inside functions that use variables that “seems” global:

```julia
function a()
    aux = Float64[]
    b = ()->begin
        push!(aux, rand())
    end
    b()
    b()
    return aux
end

julia> a()

```

I know that are other (possibly better) ways to do that, but using those variables inside those functions like they were globals will make my life very easy. Question: is there any performance penalty in this case? Where can I read more about this use case?

---

<div class="post-metadata">

**Author:** ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)\
**Post date:** [November 23, 2019, 11:07pm UTC](https://discourse.julialang.org/t/functions-inside-functions-using-global-variables/31434/2 "2019-11-23T23:07:46Z")

</div>

You’re making a closure, which is a very acceptable thing to do (unless I misunderstand your example). Closures are preformant, except for one bug in Julia: [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276)

There was a recent thread discussing work-arounds: [Avoiding Allocations with User provided Closures](https://discourse.julialang.org/t/avoiding-allocations-with-user-provided-closures/31279)

---

<div class="post-metadata">

**Author:** ![tk3369](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tk3369/32/2824_2.png) [@tk3369](https://discourse.julialang.org/u/tk3369)\
**Post date:** [November 23, 2019, 11:08pm UTC](https://discourse.julialang.org/t/functions-inside-functions-using-global-variables/31434/3 "2019-11-23T23:08:23Z")

</div>

There seems to be a small overhead comparing the two versions below. You may not need to deal with it unless you really want to optimize the code to the fullest extent.

```julia
function a1()
    aux = Float64[]
    b = ()->begin
        push!(aux, rand())
    end
    b()
    b()
    return aux
end

function a2()
    b! = (aux)->begin
        push!(aux, rand())
    end
    aux = Float64[]
    b!(aux)
    b!(aux)
    return aux
end

using BenchmarkTools
@btime a1(); # 102.432 ns (3 allocations: 144 bytes)
@btime a2(); # 96.532 ns (2 allocations: 128 bytes)

```
