# Functions, Modules, and Scope

**URL:** https://discourse.julialang.org/t/functions-modules-and-scope/32112
**Category:** General Usage
**Created:** [December 10, 2019, 6:12pm UTC](https://discourse.julialang.org/t/functions-modules-and-scope/32112 "2019-12-10T18:12:37Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![manderson](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@manderson](https://discourse.julialang.org/u/manderson)
#### Post date: [December 10, 2019, 6:12pm UTC](https://discourse.julialang.org/t/functions-modules-and-scope/32112/1 "2019-12-10T18:12:37Z")

</div>

Alright, so I have been working on an algorithm with lots of variables and lots of repeated function calls, so I put all of my functions into a module and everything works just fine. As I’m nearing the publishing of a paper on this algorithm, I’m trying to do some clean up to make things look nicer and perhaps be less confusing for people who may end up using the algorithm.

The question I have, is this, is there a way to keep my functions in a module, but not have to pass every single variable into the function every time I use it? Let me provide an example.

In the REPL I can make a function that only takes 1 variable as the input, but uses another variable without a problem.

```julia
julia> f = X -> X+Y
#9 (generic function with 1 method)

julia> Y=7;

julia> f(3)
10

```

I have numerous functions that are beyond my ability to craft in a single line but I can also use:

```julia
julia> function ff(X)
           X+Y
           end
ff (generic function with 1 method)

julia> Y=7;

julia> ff(3)
10

```

Now, I’ve got a bunch of different functions, and I don’t want them cluttering the file where the main algorithm is constructed, so I put the function into a module.

```julia
module testlib
export ff
function ff(X)
  X+Y
  end
end

```

So I load up my module to the REPL, but it no longer works. I think this what you call is a scope problem.

```julia
julia> using testlib

julia> Y=7;

julia> ff(3)
ERROR: UndefVarError: Y not defined
Stacktrace:
 [1] ff(::Int64) at C:\Users\#######\Documents\GitHub\####\testlib.jl:5
 [2] top-level scope at none:0

```

I have gotten around this by writing the functions in the module so that they take all of the variables as arguments. i.e.

```julia
module testlib
export ff
function ff(X,Y)
  X+Y
  end
end

```

This works just fine, but is not ascetically pleasing when you have massive amounts of arguments getting passed into functions. I have gone so far as to write data structures to pass these variables around, but its still unwieldy.

Ultimately, I’d like to know, is there a more proper way for me to do this?

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [December 10, 2019, 6:35pm UTC](https://discourse.julialang.org/t/functions-modules-and-scope/32112/2 "2019-12-10T18:35:55Z")

</div>

Using global variables in this way

```julia
function ff(X)
  X+Y
end

```

is a questionable approach. Unless `Y` is constant, this will hurt performance. Plus, it is not clear  
where that variable comes from, which is a big problem from readability point of view.

---

<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: [December 10, 2019, 6:50pm UTC](https://discourse.julialang.org/t/functions-modules-and-scope/32112/3 "2019-12-10T18:50:14Z")

</div>

> [@manderson](#):
>
> I have gone so far as to write data structures to pass these variables around, but its still unwieldy.

[Parameters.jl](https://github.com/mauro3/Parameters.jl) may help.

---

<div class="post-metadata">

### Author: ![PetrKryslUCSD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/petrkryslucsd/32/215825_2.png) [@PetrKryslUCSD](https://discourse.julialang.org/u/PetrKryslUCSD)
#### Post date: [December 10, 2019, 8:34pm UTC](https://discourse.julialang.org/t/functions-modules-and-scope/32112/4 "2019-12-10T20:34:17Z")

</div>

Or `Base.@kwdef`. That is probably easier, since no extra package is involved.

---

<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 11, 2019, 12:34am UTC](https://discourse.julialang.org/t/functions-modules-and-scope/32112/5 "2019-12-11T00:34:44Z")

</div>

One way around this is to define Y using a closure

```julia
module testlib
export set_Y
function set_Y(Y)
    function ff(X)
        X+Y
    end
    return ff
end
end

```

and getting the function ff back via

```julia
ff = testlib.set_Y(1)

```

---

<div class="post-metadata">

### Author: ![manderson](https://avatars.discourse-cdn.com/v4/letter/m/e47774/32.png) [@manderson](https://discourse.julialang.org/u/manderson)
#### Post date: [December 11, 2019, 12:44am UTC](https://discourse.julialang.org/t/functions-modules-and-scope/32112/6 "2019-12-11T00:44:59Z")

</div>

What I’m taking away from this is that I have been doing things the safe way by using:

```julia
function ff(X,Y)
 X+Y
end

```

Which is manageable, if a bit of a pain when lots of variables are involved. I just wanted to be sure I wasn’t missing something silly.

Thanks for the input!
