# UndefVarError with nested function passed to @cfunction

**URL:** <https://discourse.julialang.org/t/undefvarerror-with-nested-function-passed-to-cfunction/59741>\
**Category:** New to Julia\
**Created:** [April 21, 2021, 3:07pm UTC](https://discourse.julialang.org/t/undefvarerror-with-nested-function-passed-to-cfunction/59741 "2021-04-21T15:07:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![psanan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/psanan/32/30106_2.png) [@psanan](https://discourse.julialang.org/u/psanan)\
**Post date:** [April 21, 2021, 3:07pm UTC](https://discourse.julialang.org/t/undefvarerror-with-nested-function-passed-to-cfunction/59741/1 "2021-04-21T15:07:58Z")

</div>

I’m working on a project that involves wrapping Julia functions to be used as callbacks for a C library. I don’t understand the error when I run this, but I’m new enough to Julia to be confused about whether I should expect this to work:

```
function f()
    function g()::Cvoid
        return nothing
    end
    g_c = @cfunction(g, Cvoid, ());
end

```

I see `UndefVarError: g not defined` with Julia 1.5.2 running in a Jupyter notebook.

Notes:

- I see some related posts on a similar behavior with a conditionally-defined nested non-anonymous function, and [this open issue](https://github.com/JuliaLang/julia/issues/15602).
- The actual “`g`” I’m interested in is more complex, with input arguments and modifying an array defined in `f`.

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [April 22, 2021, 2:03am UTC](https://discourse.julialang.org/t/undefvarerror-with-nested-function-passed-to-cfunction/59741/2 "2021-04-22T02:03:42Z")

</div>

> [@psanan](#):
>
> `g_c = @cfunction(g, Cvoid, ());`

`g_c = @cfunction($g, Cvoid, ());`

[doc](https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/#Closure-cfunctions)
