# Use of include outside and inside function

**URL:** <https://discourse.julialang.org/t/use-of-include-outside-and-inside-function/59101>\
**Category:** New to Julia\
**Created:** [April 12, 2021, 1:24pm UTC](https://discourse.julialang.org/t/use-of-include-outside-and-inside-function/59101 "2021-04-12T13:24:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mocalvao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mocalvao/32/19318_2.png) [@mocalvao](https://discourse.julialang.org/u/mocalvao)\
**Post date:** [April 12, 2021, 1:24pm UTC](https://discourse.julialang.org/t/use-of-include-outside-and-inside-function/59101/1 "2021-04-12T13:24:31Z")

</div>

Hi there,

I have a file called `file.txt`, with the following contents:

```julia
N_questions = 4
ini_window = "2021-04-10 14:00"

```

I will use the variables `N_questions` and `ini_window` later on, in a (long) Julia script I named `include_within_function.jl`, which starts like:

```julia
using Dates

function build_df(file)
    include(file)

    if !(isinteger(N_questions) && N_questions > 0)
        error("N_questions must be a positive integer.")
    end

    dtformat = DateFormat("y-m-d H:M")
    ini_window = DateTime(ini_window, dtformat)
end

```

After declaring

```julia
file = "file.txt"
include("include_within_function.jl")

```

and then

```julia
build_df(file)

```

I get the error:

```julia
ERROR: UndefVarError: ini_window not defined

```

Sorry for the silly question, but: Why is it the variable `N_questions` is recognized (since the if loop issues no error), while the ini\_window variable is not? I am almost sure it is a matter of scope, but am not able to figure it out! I say this because, when I run the statements of the body of the `build_df` function outside it, the variable `ini_window` is defined…  
Could someone give me a nice detailed and pedagogical explanation for this behavior, with examples, if possible?  
PS: On a parenthetical note, I would also appreciate suggestions as to the best way for Julia to digest parameters or variables provided in an ascii file instead of `include`ing it, as for the `file.txt` above.

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)\
**Post date:** [April 12, 2021, 1:42pm UTC](https://discourse.julialang.org/t/use-of-include-outside-and-inside-function/59101/2 "2021-04-12T13:42:54Z")

</div>

Using `include` _inside_ a function definition isn’t going to be useful since it evaluates the contents of the file in the current global scope rather than the functions local scope. See the first sentence of it’s docstring: “Evaluate the contents of the input source file in the global scope of the containing module.” As to why the first variable is visible and the second not, my guess would be that it’s just an artifact of how the `eval` is done inside `include`, don’t go relying on it.

I’d suggest using a “real” data format for your text file, probably TOML is the simplest to get working.

Inside `config.toml`

```nohighlight
N_questions = 4
ini_window = "2021-04-10 14:00"

```

and then in do:

```julia
julia> import TOML

julia> function build_df(file)
           config = TOML.parsefile(file)

           if !(isinteger(config["N_questions"]) && config["N_questions"] > 0)
               error("N_questions must be a positive integer.")
           end

           dtformat = DateFormat("y-m-d H:M")
           ini_window = DateTime(config["ini_window"], dtformat)
       end

julia> build_df("config.toml")

```

Mixing `include` and global/local scopes together might seem to work nicely in little examples sometimes, but it’s just going to cause you pain more often than not. Just use a real data format, whether it’s TOML, or JSON or something else doesn’t matter too much.

---

<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:** [April 12, 2021, 1:47pm UTC](https://discourse.julialang.org/t/use-of-include-outside-and-inside-function/59101/3 "2021-04-12T13:47:22Z")

</div>

> [@mike](#):
>
> See the first sentence of it’s docstring: “Evaluate the contents of the input source file in the global scope of the containing module.”

I fell so proud of this [ridiculous PR](https://github.com/JuliaLang/julia/pull/39317).

---

<div class="post-metadata">

**Author:** ![mike](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike/32/39_2.png) [@mike](https://discourse.julialang.org/u/mike)\
**Post date:** [April 12, 2021, 1:49pm UTC](https://discourse.julialang.org/t/use-of-include-outside-and-inside-function/59101/4 "2021-04-12T13:49:29Z")

</div>

It’s the little details that matter the most.
