# Passing a function to an eval in my module

**URL:** https://discourse.julialang.org/t/passing-a-function-to-an-eval-in-my-module/44955
**Category:** General Usage
**Tags:** question, module
**Created:** [August 14, 2020, 7:56pm UTC](https://discourse.julialang.org/t/passing-a-function-to-an-eval-in-my-module/44955 "2020-08-14T19:56:17Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![koosp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/koosp/32/17186_2.png) [@koosp](https://discourse.julialang.org/u/koosp)
#### Post date: [August 14, 2020, 7:56pm UTC](https://discourse.julialang.org/t/passing-a-function-to-an-eval-in-my-module/44955/1 "2020-08-14T19:56:17Z")

</div>

I made a module which had a function, that calls another function inside of it to compute partial derivatives. I tested it with Base functions like `exp`, `sin` and so on and it worked, it still works. I also tested it with my own functions inside the notebook before copying all the code into a module.

Now when I import my module the function only works with Base functions, when I pass my own I get the error “UndefVarError: myfunc not defined”

I threw in the debug print in there, and the correct value for myfunc(1, 2, 3) gets printed, so the function is visible in the scope of the module once it’s been passed to the function, right? But when calling eval, it’s no longer visible. I need to use eval since I want it to work with functions that have any amount of arguments, unless there are better ways to do it, I’m open to any suggestions and knowledge about how scopes work with eval and expressions.

I read this [Scope of Variables · The Julia Language](https://docs.julialang.org/en/v1/manual/variables-and-scoping/) for any hints, but didn’t seem to find my answer, maybe I missed it.

```julia
function _partial_derivative(args::AbstractArray, idx::Integer, f::Function)
        print(f(1, 2, 3)) #still prints in Main
        args = (arg -> [[arg 0]; [0 arg]]).(args)
        args[idx] += [[0 1]; [0 0]]
        expr = ""
        for arg in args
            expr = "$expr, $arg" #make a string of my arguments
        end
        expr = expr[3:end]
        f_expr = Meta.parse("$f($expr)") #combine the function and the arguments, evaluate
        res = eval(f_expr)
        return res[1, 1], res[1, 2] #return the function and its partial derivative
end

```

---

<div class="post-metadata">

### Author: ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)
#### Post date: [August 14, 2020, 7:59pm UTC](https://discourse.julialang.org/t/passing-a-function-to-an-eval-in-my-module/44955/2 "2020-08-14T19:59:00Z")

</div>

You can `eval` into a particular module. But you almost definitely don’t want to be doing this. Rather, you want to define [closures](https://julia-lang.programmingpedia.net/en/tutorial/5724/closures).

---

<div class="post-metadata">

### Author: ![yha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yha/32/3502_2.png) [@yha](https://discourse.julialang.org/u/yha)
#### Post date: [August 14, 2020, 8:45pm UTC](https://discourse.julialang.org/t/passing-a-function-to-an-eval-in-my-module/44955/3 "2020-08-14T20:45:59Z")

</div>

> [@koosp](#):
>
> I need to use eval since I want it to work with functions that have any amount of arguments, unless there are better ways to do it

I think you’re looking for the splat operator `...` – in this case `f(args...)`.

---

<div class="post-metadata">

### Author: ![koosp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/koosp/32/17186_2.png) [@koosp](https://discourse.julialang.org/u/koosp)
#### Post date: [August 14, 2020, 8:58pm UTC](https://discourse.julialang.org/t/passing-a-function-to-an-eval-in-my-module/44955/4 "2020-08-14T20:58:07Z")

</div>

It works, never knew it can be used not just in function definitions. Thank you!

---

<div class="post-metadata">

### Author: ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)
#### Post date: [August 14, 2020, 10:11pm UTC](https://discourse.julialang.org/t/passing-a-function-to-an-eval-in-my-module/44955/5 "2020-08-14T22:11:08Z")

</div>

Also note that you pretty much never want to manipulate Julia code as strings. Manipulating code as `Expr` objects is much less of a hassle and a lot less error prone. Have a look at the metaprogramming section in the manual:  
[Metaprogramming · The Julia Language](https://docs.julialang.org/en/v1/manual/metaprogramming/).
