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

Suppose I have a function structure:

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



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

How about

function neighbor_loop!(f::Function, args...)
1 Like

Almost I think?

The issue is:

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

julia> func(nothing,1)

julia> a = 1

julia> func(nothing,a)

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

use a named tuple ?


julia> function func(f, nt::NamedTuple)
           @show f([nt.a, nt.b, nt.c])
           @show f(values(nt))
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

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

1 Like

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.

1 Like


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:

           function func(f,args...)
               return a + 1

           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)
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 :slight_smile:

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?


(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.)


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 as well.

1 Like

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