# Setting variable used by multiple functions in a module

**URL:** <https://discourse.julialang.org/t/setting-variable-used-by-multiple-functions-in-a-module/32103>\
**Category:** New to Julia\
**Created:** [December 10, 2019, 4:09pm UTC](https://discourse.julialang.org/t/setting-variable-used-by-multiple-functions-in-a-module/32103 "2019-12-10T16:09:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [December 10, 2019, 4:09pm UTC](https://discourse.julialang.org/t/setting-variable-used-by-multiple-functions-in-a-module/32103/1 "2019-12-10T16:09:10Z")

</div>

I’m trying to find the Julian way to replace the following OOP functionality: if a variable is set when an object is instantiated, then all functions in that instance have access to the variable.

It seems like [closures are the correct “Julian” way to reproduce this](https://discourse.julialang.org/t/functions-inside-functions-using-global-variables/31434). Is there a way to mimic instantiation and set a variable once, or should I expect to call `set_a` each time I use module functions which access that variable?

```julia
module foo
export set_a

function set_a(a)
    f1(x) = a + x
    f2(x) = a * x
    return f1,f2
end

function bar(x)
    f1,f2 = set_a(42)
    return f1(x) + f2(x)
end

end

```

Thanks! I hope this isn’t duplicating another existing question.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [December 11, 2019, 8:15am UTC](https://discourse.julialang.org/t/setting-variable-used-by-multiple-functions-in-a-module/32103/2 "2019-12-11T08:15:29Z")

</div>

I am not sure I fully understand what you are trying to do. If you just need a callable that has state, perhaps just make a mutable struct callable:

```julia
mutable struct HasState{T}
    a::T
end

(A::HasState)(x) = A.a + x

```

etc.

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [January 20, 2020, 10:25pm UTC](https://discourse.julialang.org/t/setting-variable-used-by-multiple-functions-in-a-module/32103/3 "2020-01-20T22:25:42Z")

</div>

Sorry for the confusing wording (and delayed response).

I am trying to pass arguments by closure to multiple functions (the “in a module” part of the question was unnecessary). I am doing so by manually adding functions to the closure and return list, e.g.,

```julia
function close_args(A)
    foo1(x) = A*x
    foo2(x) = x+A*x
    foo3(x) = 2*x+A*x
    return foo1,foo2,foo3
end

foo1,foo2,foo3 = close_args(A)

```

This seems a little cumbersome if I keep adding new functions. Is there a way to avoid this (for example, by passing a function into `close_args` instead of defining it inside)?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 21, 2020, 7:16am UTC](https://discourse.julialang.org/t/setting-variable-used-by-multiple-functions-in-a-module/32103/4 "2020-01-21T07:16:29Z")

</div>

I am still not sure what you are trying to do, but

```julia
let A = B
    x -> A*x, x -> x+A*x, x -> 2*x+A*x
end

```

would be equivalent to `close_args(B)`.

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [January 21, 2020, 8:32am UTC](https://discourse.julialang.org/t/setting-variable-used-by-multiple-functions-in-a-module/32103/5 "2020-01-21T08:32:15Z")

</div>

Thanks for the reply!

How do you extract functions from the let block? That seems pretty close to what I’d like to do - in pseudocode, something like

```julia
# define variables
A = rand(10,10)

# define a bunch of functions which inherit A thru closure
let A=A
     foo1(x) = A*x
     foo2(x) = ...
     foo3(x) = ...
     #...etc,etc
end

# call those functions outside the let block
b = foo1(rand(10))

```

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 21, 2020, 8:34am UTC](https://discourse.julialang.org/t/setting-variable-used-by-multiple-functions-in-a-module/32103/6 "2020-01-21T08:34:23Z")

</div>

They are just the value of the `let` block, eg

```julia
foo1, foo2, foo3 = let ... end

```

That said, I would really recommend defining a (mutable) `struct` and make it callable. Your future self working with this code will thank you.

---

<div class="post-metadata">

**Author:** ![jlchan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlchan/32/10958_2.png) [@jlchan](https://discourse.julialang.org/u/jlchan)\
**Post date:** [January 21, 2020, 8:50am UTC](https://discourse.julialang.org/t/setting-variable-used-by-multiple-functions-in-a-module/32103/7 "2020-01-21T08:50:26Z")

</div>

Thanks. I’ll try rewriting things around a callable state.

FWIW, I was originally trying to use closures to pass variables, so that I could avoid long lists of input arguments. The let blocks and function closures work for this, I just wanted to see if I could avoid having to explicitly listing all the closed functions as output values.
