# How to pass all the variables defined within a function to a nested function?

**URL:** <https://discourse.julialang.org/t/how-to-pass-all-the-variables-defined-within-a-function-to-a-nested-function/94418>\
**Category:** General Usage\
**Tags:** scope, function\
**Created:** [February 10, 2023, 4:51pm UTC](https://discourse.julialang.org/t/how-to-pass-all-the-variables-defined-within-a-function-to-a-nested-function/94418 "2023-02-10T16:51:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fipelle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fipelle/32/4772_2.png) [@fipelle](https://discourse.julialang.org/u/fipelle)\
**Post date:** [February 10, 2023, 4:51pm UTC](https://discourse.julialang.org/t/how-to-pass-all-the-variables-defined-within-a-function-to-a-nested-function/94418/1 "2023-02-10T16:51:29Z")

</div>

Hi there,

I am trying to pass all the variables defined within a function (say `foo`) to a nested function (say `bar`) as arguments. Something along the lines of

```julia
function foo(arg1, arg2; kwarg1=1, kwarg2=2) 
    # do something
    bar(all variables within foo including arg1, arg2, kwarg1, kwarg2)
end

```

Is there a simple way to do it?

---

<div class="post-metadata">

**Author:** ![Ahmed\_Salih](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ahmed_salih/32/206579_2.png) [@Ahmed\_Salih](https://discourse.julialang.org/u/Ahmed_Salih)\
**Post date:** [February 10, 2023, 5:15pm UTC](https://discourse.julialang.org/t/how-to-pass-all-the-variables-defined-within-a-function-to-a-nested-function/94418/2 "2023-02-10T17:15:48Z")

</div>

Perhaps `return Base.@locals` ?

Not the best way though, maybe pack them into a dict using `Parameters`, and then unpack them inside the function?

Kind regards

---

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [February 11, 2023, 2:33am UTC](https://discourse.julialang.org/t/how-to-pass-all-the-variables-defined-within-a-function-to-a-nested-function/94418/3 "2023-02-11T02:33:14Z")

</div>

Why do you wish to pass the local variables as arguments, when you can simply allow the inner function to capture them from its enclosing scope?

---

<div class="post-metadata">

**Author:** ![jbu](https://avatars.discourse-cdn.com/v4/letter/j/a8b319/32.png) [@jbu](https://discourse.julialang.org/u/jbu)\
**Post date:** [February 11, 2023, 4:11am UTC](https://discourse.julialang.org/t/how-to-pass-all-the-variables-defined-within-a-function-to-a-nested-function/94418/4 "2023-02-11T04:11:41Z")

</div>

Adding to @uniment’s answer, the documentation on [Scope of Variables](https://docs.julialang.org/en/v1/manual/variables-and-scoping/) might be helpful:

> A scope nested inside another scope can “see” variables in all the outer scopes in which it is contained.

> A new local scope is introduced by most code blocks…If such a block is syntactically nested inside of another local scope, the scope it creates is nested inside of all the local scopes that it appears within, which are all ultimately nested inside of the global scope of the module in which the code is evaluated. Variables in outer scopes are visible from any scope they contain — meaning that they can be read and written in inner scopes…
