# Call Julia function with keywords from C++

**URL:** <https://discourse.julialang.org/t/call-julia-function-with-keywords-from-c/20981>\
**Category:** General Usage\
**Tags:** c\
**Created:** [February 19, 2019, 5:53pm UTC](https://discourse.julialang.org/t/call-julia-function-with-keywords-from-c/20981 "2019-02-19T17:53:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 19, 2019, 5:53pm UTC](https://discourse.julialang.org/t/call-julia-function-with-keywords-from-c/20981/1 "2019-02-19T17:53:50Z")

</div>

Hi guys!

Let’s say I have Julia function:

```nohighlight
function teste(a::Int,b::Int; c = 1.0, d = 1.0, e = 1.0)
    return (a+b)^(c+d+e)
end

```

How can I call it from C++ modifying the keyword `d`?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 19, 2019, 8:28pm UTC](https://discourse.julialang.org/t/call-julia-function-with-keywords-from-c/20981/2 "2019-02-19T20:28:28Z")

</div>

The only very dirty workaround I could think was to create a dummy function like:

```julia
teste_dummy(a,b,c) = teste(a,b;c=c)

```

Is it really the only way?

---

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [February 20, 2019, 3:08pm UTC](https://discourse.julialang.org/t/call-julia-function-with-keywords-from-c/20981/3 "2019-02-20T15:08:29Z")

</div>

I found a better way by using the `Core.kwfunc`:

```julia
#include <iostream>

extern "C" {
#include <julia.h>
}

int main(int argv, char* argc[])
{
    jl_init();

    jl_function_t* teste = jl_eval_string("function teste(a;c = 1.0) \
            a+c \
            end");

    jl_function_t* kwsorter = jl_eval_string("Core.kwfunc");
    jl_function_t* testek = jl_call1(kwsorter,teste);

    jl_value_t* a = nullptr;
    jl_value_t* b = nullptr;
    jl_value_t* c = nullptr;

    JL_GC_PUSH3(a,b,c);

    a = (jl_value_t*)jl_box_float64(1.0);
    b = (jl_value_t*)jl_box_float64(1.0);
    c = (jl_value_t*)jl_eval_string("(c = 3.0,)");

    jl_value_t* ret = (jl_value_t*)jl_call3(testek,c,teste,a);

    std::cout << jl_unbox_float64(ret) << std::endl;

    JL_GC_POP();

    jl_atexit_hook(0);

    return 0;
}

```

Just need to find a way to create `NamedTuples` on C now.

---

<div class="post-metadata">

**Author:** ![mkitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkitti/32/12459_2.png) [@mkitti](https://discourse.julialang.org/u/mkitti)\
**Post date:** [November 28, 2020, 6:03pm UTC](https://discourse.julialang.org/t/call-julia-function-with-keywords-from-c/20981/4 "2020-11-28T18:03:03Z")

</div>

Adding a cross reference to the issue and my tentative answer:  
[https://github.com/JuliaLang/julia/issues/31119#issuecomment-735109230](https://github.com/JuliaLang/julia/issues/31119#issuecomment-735109230)
