# Passing all variables of an outer function to an inner function?

**URL:** <https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726>\
**Category:** Performance\
**Created:** [April 9, 2024, 1:22pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726 "2024-04-09T13:22:28Z")\
**Posts on this page:** 10\
**Page:** 1

<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:** [April 9, 2024, 1:22pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726/1 "2024-04-09T13:22:28Z")

</div>

Suppose I have a function structure:

```julia
function NeighborLoop!(f::Function, a,b,c,d)

f(a,b,c,d)

end

```

In this case I have to double specify all the variables. Is there a smart and easy way to pass all variables from `NeighborLoop!` into `f` without having to double specify?

I would like to have this code structure, since in my real use case the `NeighborLoop!` is inside of a package module.

Kind regards

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [April 9, 2024, 1:28pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726/2 "2024-04-09T13:28:13Z")

</div>

How about

```julia
function neighbor_loop!(f::Function, args...)
    f(args...)
end

```

---

<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:** [April 9, 2024, 1:41pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726/3 "2024-04-09T13:41:07Z")

</div>

Almost I think?

The issue is:

```julia
julia> function func(f,args...)
       for arg in args
           println(args)
       end
       end
func (generic function with 1 method)

julia> func(nothing,1)
(1,)

julia> a = 1
1

julia> func(nothing,a)
(1,)

```

I would like to pass in the known name i.e. in this case `a`. So it should be arbitrary number of variables and one should be able to call them using their variable name.

Kind regards

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [April 9, 2024, 1:45pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726/4 "2024-04-09T13:45:59Z")

</div>

use a named tuple ?

Example:

```julia
julia> function func(f, nt::NamedTuple)
           @show f([nt.a, nt.b, nt.c])
           @show f(values(nt))
       end
func (generic function with 1 method)

julia> func(sum, (;a = 1, b= 2, c =3, d = 4))
f([nt.a, nt.b, nt.c]) = 6
f(values(nt)) = 10
10

```

But if the keywords are statically known, probably it’s better to pass them as keyword arguments.

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [April 9, 2024, 1:53pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726/5 "2024-04-09T13:53:17Z")

</div>

> [@Ahmed\_Salih](#):
>
> ```julia
> for arg in args
> println(args)
> end
> 
> ```

Not sure what your goal is, but you seem to have a typo and need `println(arg)` instead. That’s why you’re seeing a tuple instead of a number printed out, in your previous post.

---

<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:** [April 9, 2024, 2:01pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726/6 "2024-04-09T14:01:16Z")

</div>

> [@Ahmed\_Salih](#):
>
> ```julia
> function func(f,args...)
> for arg in args
> println(args)
> end
> end
> 
> ```

Thanks!

I want to do this basically, refer to `a` inside of a function without passing it in as a variable, but through `args`. It seems to work now:

```julia
  let
           function func(f,args...)
               return a + 1
           end

           a = 1
           println("value of a starts as: ", a)

           b = func(nothing, a)

           println("value of b is: ", b)
           println("value of a ends as: ", a)
       end
value of a starts as: 1
value of b is: 2
value of a ends as: 1

```

So I think I just try to modify my actual code to this pattern and report back 🙂

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [April 9, 2024, 2:25pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726/7 "2024-04-09T14:25:14Z")

</div>

This example does not use `args` at all. You post shere read somewhat misguided/confused but I cannot make out what you really wanted to ask. Can you step back on step and explain why you asked the original question, i.e. what you want to achieve in the end?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 9, 2024, 2:30pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726/8 "2024-04-09T14:30:19Z")

</div>

(In general, if you have a long list of arguments that you are passing around between lots of functions, e.g. shared parameters for a computational model, you might want to consider defining a data structure to encapsulate them.)

---

<div class="post-metadata">

**Author:** ![filchristou](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/filchristou/32/26760_2.png) [@filchristou](https://discourse.julialang.org/u/filchristou)\
**Post date:** [April 9, 2024, 2:46pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726/9 "2024-04-09T14:46:09Z")

</div>

I generally wouldn’t suggest you to use this pattern a lot. Here `func` accesses `a` as a global variable. Having global variables is slow (if they are immutable, defining them as `const` is already a lot faster). But it’s not only about speed; having functions depending on a global state that can change at any point is a fragile pattern. Although sometimes unavoidable, most of the times there is a better design. E.g., have a look at [Parameters.jl](https://github.com/mauro3/Parameters.jl) as well.

---

<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:** [April 9, 2024, 3:06pm UTC](https://discourse.julialang.org/t/passing-all-variables-of-an-outer-function-to-an-inner-function/112726/10 "2024-04-09T15:06:11Z")

</div>

Thank you for the feedback everyone!

I will give it a little bit of a think and see how else I can structure my problem, thanks!

Kind regards
