# Load functions from files dynamically without including them in global scope

**URL:** https://discourse.julialang.org/t/load-functions-from-files-dynamically-without-including-them-in-global-scope/51248
**Category:** General Usage
**Tags:** question, module
**Created:** [December 4, 2020, 12:38pm UTC](https://discourse.julialang.org/t/load-functions-from-files-dynamically-without-including-them-in-global-scope/51248 "2020-12-04T12:38:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![akhil\_teja](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/akhil_teja/32/15235_2.png) [@akhil\_teja](https://discourse.julialang.org/u/akhil_teja)
#### Post date: [December 4, 2020, 12:38pm UTC](https://discourse.julialang.org/t/load-functions-from-files-dynamically-without-including-them-in-global-scope/51248/1 "2020-12-04T12:38:17Z")

</div>

I want to dynamically include functions from files based on condition.

Below code gives you an Idea.

File1.jl

```julia
function foo()
    println("In File1.jl")
end

```

File2.jl

```julia
function foo()
    println("In File2.jl")
end

```

Main.jl

```julia
function main(get_func_from)
    if get_func_from=="File1.jl"
        include("File1.jl")
        foo()
    end
end

```

Please note that I don’t want to include File1, File2 under global scope and want to call foo and include file only when needed.

---

<div class="post-metadata">

### Author: ![mauro3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mauro3/32/292_2.png) [@mauro3](https://discourse.julialang.org/u/mauro3)
#### Post date: [December 4, 2020, 12:47pm UTC](https://discourse.julialang.org/t/load-functions-from-files-dynamically-without-including-them-in-global-scope/51248/2 "2020-12-04T12:47:23Z")

</div>

You cannot do that. `include` works only at global scope for the same reason as `eval` only works at global scope (`include` really is eval-this-file), namely performance. If you could eval into local scope, Julia’s compilation-scheme would not work anymore. This limitation is really at the heart why Julia is fast.

Try to formulate your problem as a dispatch problem instead. Or just call into different functions in a if-else statement.
