# @cfunction cannot find function defined in the same module

**URL:** <https://discourse.julialang.org/t/cfunction-cannot-find-function-defined-in-the-same-module/28658>\
**Category:** General Usage\
**Created:** [September 11, 2019, 6:15pm UTC](https://discourse.julialang.org/t/cfunction-cannot-find-function-defined-in-the-same-module/28658 "2019-09-11T18:15:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pemryan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pemryan/32/1237_2.png) [@pemryan](https://discourse.julialang.org/u/pemryan)\
**Post date:** [September 11, 2019, 6:15pm UTC](https://discourse.julialang.org/t/cfunction-cannot-find-function-defined-in-the-same-module/28658/1 "2019-09-11T18:15:12Z")

</div>

I’d like to use a function f() defined in a module as a callback in another function g() in the same module. However, I encountered this error:

ERROR: LoadError: UndefVarError: f not defined.

moving the definition of function f() before the definition g() does make it work.

I googled a bit and tried adding ‘$’ before f() in @cfunction, by doing this, the file can be included without error, but still fail to run and the REPL complained about type mismatch between ::Base.CFunction and ::Ptr{Nothing}.

Please help me out this situation. Thank you.

Julia Version 1.2.0  
Commit c6da87ff4b (2019-08-20 00:03 UTC)  
Platform Info:  
OS: Windows (x86\_64-w64-mingw32)  
CPU: Intel(R) Xeon(R) CPU X5670 @ 2.93GHz  
WORD\_SIZE: 64  
LIBM: libopenlibm  
LLVM: libLLVM-6.0.1 (ORCJIT, westmere)

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [September 11, 2019, 9:03pm UTC](https://discourse.julialang.org/t/cfunction-cannot-find-function-defined-in-the-same-module/28658/2 "2019-09-11T21:03:12Z")

</div>

Hi,  
I can’t quite tell what you’re trying to do. Are you trying to pass the function f as an input to the function g? Or are you just trying to call f from inside g? It’s a bit hard to help without knowing a little more about your situation. Are you able to post a minimal working example?

Also, is this all happening inside julia or are you trying to call julia from C? I’m asking because you’ve mentioned the use of `@cfunction`.

---

<div class="post-metadata">

**Author:** ![pemryan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pemryan/32/1237_2.png) [@pemryan](https://discourse.julialang.org/u/pemryan)\
**Post date:** [September 11, 2019, 10:00pm UTC](https://discourse.julialang.org/t/cfunction-cannot-find-function-defined-in-the-same-module/28658/3 "2019-09-11T22:00:39Z")

</div>

Thank you for your reply. I was trying to use a self-defined function as a callback for a C-function.  
Here is a example that can re produce the error:

```julia
module TestModule

function TestCallBackFunc(f::Ptr{Cvoid}, x, y)
    return ccall(f, Cint, (Cint, Cint), x, y)
end

function g()
    return TestCallBackFunc(@cfunction(f, Cint, (Cint, Cint)), 1, 2)
end

function f(a::Cint, b::Cint)::Cint
    return (a > b) ? -1 : 1
end

end

```

include this file would raise error:

```julia
julia> include("TestModule.jl")
ERROR: LoadError: UndefVarError: f not defined

```

However, simply moving the definition of f() would make it work.

```julia
module TestModule

function f(a::Cint, b::Cint)::Cint
    return (a > b) ? -1 : 1
end

function TestCallBackFunc(f::Ptr{Cvoid}, x, y)
    return ccall(f, Cint, (Cint, Cint), x, y)
end

function g()
    return TestCallBackFunc(@cfunction(f, Cint, (Cint, Cint)), 1, 2)
end

end

```

```julia
julia> include("TestModule.jl")
Main.TestModule
julia> TestModule.g()
1

```

---

<div class="post-metadata">

**Author:** ![Pbellive](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pbellive/32/3604_2.png) [@Pbellive](https://discourse.julialang.org/u/Pbellive)\
**Post date:** [September 12, 2019, 12:57am UTC](https://discourse.julialang.org/t/cfunction-cannot-find-function-defined-in-the-same-module/28658/4 "2019-09-12T00:57:28Z")

</div>

~~I think that is the intended behaviour. I think you need to define the function `f` before referencing it, even if it’s in the same module.~~  
Edit: Interesting. For regular julia functions the following works

```julia
julia> module myMod

       g(x) = f(x) + 2
       f(x) = x^2
       end
Main.myMod

julia> using .myMod

julia> myMod.g(2)
6

```

I’m not sure why the `@cfunction` macro `f` to be defined before it’s referenced. Interesting. In any event, my workaround still works:

You don’t need to define all the methods you need in advance. So if for some reason you want/need to define your `f(a::Cint, b::Cint)::Cint` method after your `g()` method you can do the following

```julia
julia> module TestModule
       export f,g
       function f end
       function TestCallBackFunc(f::Ptr{Cvoid}, x, y)
           return ccall(f, Cint, (Cint, Cint), x, y)
       end

       function g()
           return TestCallBackFunc(@cfunction(f, Cint, (Cint, Cint)), 1, 2)
       end

       function f(a::Cint, b::Cint)::Cint
           return (a > b) ? -1 : 1
       end
       end

Main.TestModule

julia> using .TestModule

julia> g()
1

```
