# Includes and loops

**URL:** <https://discourse.julialang.org/t/includes-and-loops/54752>\
**Category:** New to Julia\
**Created:** [February 6, 2021, 7:43pm UTC](https://discourse.julialang.org/t/includes-and-loops/54752 "2021-02-06T19:43:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benoit\_Gaudeul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benoit_gaudeul/32/21256_2.png) [@Benoit\_Gaudeul](https://discourse.julialang.org/u/Benoit_Gaudeul)\
**Post date:** [February 6, 2021, 7:43pm UTC](https://discourse.julialang.org/t/includes-and-loops/54752/1 "2021-02-06T19:43:52Z")

</div>

I have defined some code (generation of a mesh) in a file “mesh.jl” that I’m trying to include in an other one. The command `include("mesh.jl")` worked fine. To draw a convergence curve I tried to use it into a loop and get the error “UndefVarError: k not defined”.

Here is a MWE:

```julia
for i=1:10
	k=3
	include("bug2.jl")
end

```

and bug2.jl is a one liner:  
`N=2^k`

How could I fix this?

Thanks for reading this far,  
Best,  
Benoît

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [February 6, 2021, 7:52pm UTC](https://discourse.julialang.org/t/includes-and-loops/54752/2 "2021-02-06T19:52:20Z")

</div>

This is not a bug.

My understanding is that `include` operates in the global scope using essentially the same mechanisms as `eval`. By design, Julia is not intended for this sort of locally scoped file inclusion stuff because it’s just generally a bad way to write code.

If you want to do something like this, you should change `bug2.jl` to contain a function:

```julia
N(k) = 2^k

```

and then write

```julia
include("bug2.jl")

for i=1:10
	k=3
    N(k)
end

```

---

<div class="post-metadata">

**Author:** ![Benoit\_Gaudeul](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/benoit_gaudeul/32/21256_2.png) [@Benoit\_Gaudeul](https://discourse.julialang.org/u/Benoit_Gaudeul)\
**Post date:** [December 12, 2023, 4:56pm UTC](https://discourse.julialang.org/t/includes-and-loops/54752/3 "2023-12-12T16:56:46Z")

</div>

I took Mason’s advice to heart, and ended up not needing to do this dirty include in loop for several years. I do now (the call in a loop is done for debugging, otherwise it is a true script that will be run only once per julia session), thus I have created this small function that does it:

```julia
using Printf
using Random

function include_as_function(scriptname,argsname=[])

	header=@sprintf("function %s(",scriptname)
	for n in argsname
		header *= n * ","
	end
	header=chop(header)
	header*=")\n"

	functionfile=@sprintf("%s/%s_%s.jl",pwd(),scriptname,randstring(24))
	scriptfile=@sprintf("%s/%s.jl",pwd(),scriptname)

	
	s=open(functionfile,"a+")
	write(s,header)
	write(s,read(scriptfile))
	write(s,"end")
	close(s)
	
	include(functionfile)
	rm(functionfile)
end

```
