# Accessing variables constructed within another function

**URL:** <https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897>\
**Category:** General Usage\
**Created:** [August 17, 2022, 9:00pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897 "2022-08-17T21:00:18Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![structural](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@structural](https://discourse.julialang.org/u/structural)\
**Post date:** [August 17, 2022, 9:00pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/1 "2022-08-17T21:00:18Z")

</div>

I need to create many variables within a function and input these data into numerous functions which conduct operations on the data. How can I do this without declaring the variables as globals? As I understand it variables defined within a function are not accessible outside that function unless they are defined as globals. I have 50+ variables which vary in dimensions (i.e., I have scalars, vectors, and matrices).

Here’s a toy example of this issue. I do not know how to access the three variables defined within `prepare_data()` in the functions that follow it.

```julia
function prepare_data()
# below are two examples of variables constructed. The actual function creates 50 or so variables which vary in dimension
    data = readdlm("data.csv", ',', skipstart=1, Float64)
    var1 = data[:, 1]
    var1_index = findall(var1.==1)
    var1_dummy = (var1.==var1')
end

function dosomething1()
    # construct t as a function of the data in prepare_data() and return
    t = var1[var1_index]
    return t
end

function dosomething2()
    # use data from prepare_data() again
    t = dosomething1() - var1[var1_index]
    return t
end

```

---

<div class="post-metadata">

**Author:** ![structural](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@structural](https://discourse.julialang.org/u/structural)\
**Post date:** [August 17, 2022, 9:30pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/2 "2022-08-17T21:30:38Z")

</div>

I guess a workaround would be to store (without the function declaration) the code that creates the variables in a different file (say `prepare_data.jl`) and include it later in the main file.

So save the following in `prepare_data.jl`

```julia
const data = readdlm("data.csv", ',', skipstart=1, Float64)
const var1 = data[:, 1]
const var1_index = findall(var1.==1)
const var1_dummy = (var1.==var1')

```

call it in the master file as `include("prepare_data.jl")`. I can then go about my calculations since the variables defined in `prepare_data.jl` are accessible in the rest of the document.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 17, 2022, 9:31pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/3 "2022-08-17T21:31:29Z")

</div>

That is literally declaring them as globals.  
`include` (like `eval`) always acts at global scope

---

<div class="post-metadata">

**Author:** ![structural](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@structural](https://discourse.julialang.org/u/structural)\
**Post date:** [August 17, 2022, 9:39pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/4 "2022-08-17T21:39:29Z")

</div>

including them allows me to declare them as `const`s so that seems an improvement.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 17, 2022, 9:43pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/5 "2022-08-17T21:43:29Z")

</div>

probably just stick them all in a named tuple.  
and return that from the function.  
which can be done by writing `(; a, b, c)`  
and then accessed as `x.a` etc after you assign that return to `x`.

But also consider restructuring your code to not need to do this.

Its rare that real functions needs to deal with more than a handful of variables.  
Once appropriately scoped into nice distinct functions with clear purpose.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 17, 2022, 9:44pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/6 "2022-08-17T21:44:05Z")

</div>

I don’t understand your example, but this sounds exactly like what dictionaries are for, and _definitely_ not something globals are good for.

---

<div class="post-metadata">

**Author:** ![blackeneth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blackeneth/32/10353_2.png) [@blackeneth](https://discourse.julialang.org/u/blackeneth)\
**Post date:** [August 17, 2022, 9:51pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/7 "2022-08-17T21:51:49Z")

</div>

How about returning a dictionary from the first function?

```julia
xvars=Dict()
xvars["data"]= readdlm("data.csv", ',', skipstart=1, Float64)
xvars["var1"]= data[:, 1]
xvars["var1_index"] = findall(var1.==1)
xvars["var1_dummy"] = (var1.==var1')

return(xvars)

```

---

<div class="post-metadata">

**Author:** ![structural](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@structural](https://discourse.julialang.org/u/structural)\
**Post date:** [August 17, 2022, 9:54pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/8 "2022-08-17T21:54:47Z")

</div>

Yes I can take a look at dictionaries. Would using dictionaries improve performance compared to declaring every variable as a const?

---

<div class="post-metadata">

**Author:** ![structural](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@structural](https://discourse.julialang.org/u/structural)\
**Post date:** [August 17, 2022, 9:56pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/9 "2022-08-17T21:56:47Z")

</div>

would sticking them in tuples improve performance compared to const?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 17, 2022, 11:06pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/10 "2022-08-17T23:06:45Z")

</div>

I am not sure how these two compare to `const global`s, but surely the `NamedTuple` will have better performance than `Dict`s unless you need to frequently change the values in the `NamedTuple` fields.

---

<div class="post-metadata">

**Author:** ![structural](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@structural](https://discourse.julialang.org/u/structural)\
**Post date:** [August 17, 2022, 11:36pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/11 "2022-08-17T23:36:24Z")

</div>

The variables do not change. They are data that i need to constantly reference for each iteration of a maximum likelihood estimation.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [August 17, 2022, 11:53pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/12 "2022-08-17T23:53:57Z")

</div>

I sincerely would go with the `NamedTuple` solution then.

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 18, 2022, 5:57pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/13 "2022-08-18T17:57:17Z")

</div>

Not performance no.  
But code readability yes

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [August 18, 2022, 6:50pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/14 "2022-08-18T18:50:19Z")

</div>

If these variables are going to be consistent over time, then you may even consider creating an immutable struct type rather than a `NamedTuple`. You can just pass this struct around.

Rather than having one big struct, you could create smaller structs, and then build the larger struct out of those structs. Then you might not need to pass the entire struct. You could just pass the substruct around.

---

<div class="post-metadata">

**Author:** ![structural](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@structural](https://discourse.julialang.org/u/structural)\
**Post date:** [August 18, 2022, 9:31pm UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/15 "2022-08-18T21:31:31Z")

</div>

Thanks for all the replies. I ran into a memory issue and I am not sure if it is due to the use of `const`. The OS automatically kills the session about 4 hours in citing memory issues. Would the use of `NamedTuple`, `struct`, or dictionaries reduce the memory used?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [August 19, 2022, 11:54am UTC](https://discourse.julialang.org/t/accessing-variables-constructed-within-another-function/85897/16 "2022-08-19T11:54:11Z")

</div>

not directly. But it would mean it was possible for it to go out of scope and thus be garbage collected.

More large scale restructuring of your code may allow different parts to go out of scope at different times, and thus help more
