# Using Multiple Dispatch with Keyword Arguments

**URL:** <https://discourse.julialang.org/t/using-multiple-dispatch-with-keyword-arguments/87343>\
**Category:** New to Julia\
**Tags:** question, multidispatch\
**Created:** [September 16, 2022, 3:50am UTC](https://discourse.julialang.org/t/using-multiple-dispatch-with-keyword-arguments/87343 "2022-09-16T03:50:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Snowy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/snowy/32/36765_2.png) [@Snowy](https://discourse.julialang.org/u/Snowy)\
**Post date:** [September 16, 2022, 3:50am UTC](https://discourse.julialang.org/t/using-multiple-dispatch-with-keyword-arguments/87343/1 "2022-09-16T03:50:20Z")

</div>

Hi all,  
Still a new Julia user here, but hoping to find some help. I’m building an application that will take in user-provided information and process it. There are a few required arguments from the user, and some optional. Given we don’t know exactly what the user will upload, I thought Julia’s multiple dispatch would come in handy, but I’m having trouble with it. Here’s an example code:

```julia
using DataFrames
using Statistics
my_dataset=DataFrame(Number=(1:6), Gender=["M", "F", "F", "M","M","M"], State=["Texas","Oklahoma","Texas","Texas","Oklahoma","Texas"])

function my_function(;dataset,calc)
    println(mean(dataset[:,calc]))
end

function my_function(;dataset,calc,by_group)
    groups = unique(dataset[:,by_group])
    df1 = dataset
    for g in groups
        print("working on $g")
        dataset = filter(row->row[by_group]==g,df1)
        println(mean(dataset[:,calc]))
    end
end

```

At this point, I thought if I run the function:

```julia
run=my_function(
    dataset=my_dataset,
    calc="Number" #or add ,by_group=nothing
)

```

It would run the **first** method

I thought if I run:

```julia
run=my_function(
    dataset=my_dataset,
    calc="Number",
    by_group="Gender"
)

```

Multi-dispatch would call the **second** method.

What actually happens is I get an error if I only run my\_function with dataset and calc defined:  
`ERROR: UndefKeywordError: keyword argument by_group not assigned`

If I run my\_function with dataset, calc, and by\_group defined, I get the intended result (second method is called): `working on M4.0 working on F2.5`

Is there a way to have multidispatch work based on arguments? All the examples I’ve seen online are just for changing the argument types (e.g. x::Int vs x::String). I’ve tried playing around by setting the first function to have by\_group as an optional argument, but still can’t seem to get the intended result.

Also, please note that this is just an example… I know there’s better ways to calc a mean of a group rather than looping through a dataframe 🙂

Thanks for your help!  
Snowy

---

<div class="post-metadata">

**Author:** ![briochemc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/briochemc/32/4209_2.png) [@briochemc](https://discourse.julialang.org/u/briochemc)\
**Post date:** [September 16, 2022, 4:09am UTC](https://discourse.julialang.org/t/using-multiple-dispatch-with-keyword-arguments/87343/2 "2022-09-16T04:09:00Z")

</div>

This looks similar to [How can kwargs be used in multiple dispatch](https://discourse.julialang.org/t/how-can-kwargs-be-used-in-multiple-dispatch/86790). In short, multiple dispatch does not apply to keyword arguments (“kwargs”), but you can maybe work around it.

---

<div class="post-metadata">

**Author:** ![vettert](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vettert/32/30599_2.png) [@vettert](https://discourse.julialang.org/u/vettert)\
**Post date:** [September 16, 2022, 4:11am UTC](https://discourse.julialang.org/t/using-multiple-dispatch-with-keyword-arguments/87343/3 "2022-09-16T04:11:04Z")

</div>

This would work as intended if you use positional arguments rather than keyword arguments. The “drawback” is that the order of the arguments has to be right…

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [September 16, 2022, 5:28am UTC](https://discourse.julialang.org/t/using-multiple-dispatch-with-keyword-arguments/87343/4 "2022-09-16T05:28:17Z")

</div>

For this example, you could define

```julia
function my_function(;dataset,calc, by_group = nothing)
    by_group === nothing && return println(mean(dataset[:,calc]))
    groups = unique(dataset[:,by_group])
    df1 = dataset
    for g in groups
        print("working on $g")
        dataset = filter(row->row[by_group]==g,df1)
        println(mean(dataset[:,calc]))
    end
end

```

Another option is to define

```julia
 my_function(;dataset,calc, by_group = nothing) = my_function(dataset,calc, by_group)

function my_function(dataset, calc, ::Nothing)
    println(mean(dataset[:,calc]))
end

function my_function(dataset,calc,by_group)
    groups = unique(dataset[:,by_group])
    df1 = dataset
    for g in groups
        print("working on $g")
        dataset = filter(row->row[by_group]==g,df1)
        println(mean(dataset[:,calc]))
    end
end

```

i.e., use the keyword function only to move the arguments to positional arguments, which then use dispatch to call the correct method

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 16, 2022, 5:36am UTC](https://discourse.julialang.org/t/using-multiple-dispatch-with-keyword-arguments/87343/5 "2022-09-16T05:36:32Z")

</div>

FYI: I fixed your thread title, since this question is about keyword arguments, not optional arguments. Optional arguments are positional, but has a default value specified.
