# How to define Parameters locally in Sub julia files?

**URL:** <https://discourse.julialang.org/t/how-to-define-parameters-locally-in-sub-julia-files/84886>\
**Category:** General Usage\
**Tags:** question\
**Created:** [July 27, 2022, 6:34pm UTC](https://discourse.julialang.org/t/how-to-define-parameters-locally-in-sub-julia-files/84886 "2022-07-27T18:34:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 27, 2022, 6:34pm UTC](https://discourse.julialang.org/t/how-to-define-parameters-locally-in-sub-julia-files/84886/1 "2022-07-27T18:34:26Z")

</div>

Below is just an MWE for the concept (I am using dummy equations).  
I have main code in `mani.jl` which includes another file `subi.jl`. The main code calls function `ini(args)` to calculate `mat` which is useful only in function `update` (located in `subi.jl`).  
Since `update` is being called within `for loop` and transferring the argument `mat` at each iteration could be costly (when it is very big). So, is there a way to let `ini()` creates and defines `mat` only inside the file `subi.jl` (maybe by adding some lines), so I can then call only by two arguments, i.e., `update(R,i)`

```julia
###################### subi.jl ######################
using SparseArrays, LinearAlgebra

function ini(data)
       mat = sparse(1:length(data), [4,6,7,7,8,9], d);
       mat;
end

function update(R,mat,i)
       R .+= mat .* i;
end

###################### maini.jl ######################
using SparseArrays, LinearAlgebra
include("subi.jl");
d = [10,20,30,40,50,60];
mat = ini(d)
R = copy(mat);

function run(mat,R)
       for i in 1:100
              update(R,mat,i)
       end
end

run(mat,R);

```

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [July 27, 2022, 6:52pm UTC](https://discourse.julialang.org/t/how-to-define-parameters-locally-in-sub-julia-files/84886/2 "2022-07-27T18:52:13Z")

</div>

Julia passes non-`isbits` values (like sparse arrays) by reference, not by value, so no real cost is incurred by passing `mat` as an argument. Is your concern only conceptual, or have you seen some performance deficit when benchmarking?

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 27, 2022, 7:27pm UTC](https://discourse.julialang.org/t/how-to-define-parameters-locally-in-sub-julia-files/84886/3 "2022-07-27T19:27:03Z")

</div>

Thanks for your comment.  
It is only conceptual as I thought it will transfer the arguments which will consumes time.

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [July 27, 2022, 7:49pm UTC](https://discourse.julialang.org/t/how-to-define-parameters-locally-in-sub-julia-files/84886/4 "2022-07-27T19:49:25Z")

</div>

As an example, let’s try a function that randomly selects an element from an array and calculates its square root:

```julia
julia> f(x) = sqrt(rand(x))
f (generic function with 1 method)

```

It takes basically the same amount of time for arrays of either 3 or 30,000 elements:

```julia
julia> @btime f(v) setup = (v = rand(3));
  13.500 ns (0 allocations: 0 bytes)

julia> @btime f(v) setup = (v = rand(30_000));
  14.729 ns (0 allocations: 0 bytes)

```

---

<div class="post-metadata">

**Author:** ![Amro](https://avatars.discourse-cdn.com/v4/letter/a/4491bb/32.png) [@Amro](https://discourse.julialang.org/u/Amro)\
**Post date:** [July 27, 2022, 8:00pm UTC](https://discourse.julialang.org/t/how-to-define-parameters-locally-in-sub-julia-files/84886/5 "2022-07-27T20:00:34Z")

</div>

Am, maybe the case of 30000 elements will be always slower?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [July 27, 2022, 8:06pm UTC](https://discourse.julialang.org/t/how-to-define-parameters-locally-in-sub-julia-files/84886/6 "2022-07-27T20:06:30Z")

</div>

Well, it’s 10% slower (probably due to cache behavior), not 10,000 times slower. Hopefully that’s enough to demonstrate that Julia is passing the array by reference.
