# Define f with keyword arguments: function f does not accept keyword arguments

**URL:** https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186
**Category:** New to Julia
**Created:** [May 13, 2019, 7:45pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186 "2019-05-13T19:45:41Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)
#### Post date: [May 13, 2019, 7:45pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/1 "2019-05-13T19:45:42Z")

</div>

Hi,  
I thought this may have be a documentation issue, but now I think I have misunderstood.  
I’m following the [optional and keyword arguments](https://docs.julialang.org/en/v1/manual/methods/index.html#Note-on-Optional-and-keyword-Arguments-1) in the docs.  
I was surprised by the following:

```julia
julia> f(a=1,b=2)=1+2b
f (generic function with 3 methods) 
julia> f(b=1,a=2)
ERROR: function f does not accept keyword arguments

```

It seems that I mistook the example to be referring to optional and keyword arguments, when it is only an example of optional arguments - that is fine.  
However, the error message implanted in my mind that this is definitely (unambiguously) not a function that accepts keywords.

After some digging I realized I had thought I was looking at something like this definition (update: please see the [comment below](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/13)):

```julia
julia> f(; a=1,b=2)=2a+4b
f (generic function with 3 methods) # **See the update below**
julia> f(b=1,a=2)
8

```

Now, in the following, I expected to get a ambiguity error instead of `10`

```julia
julia> f()
10

```

On reflection I suppose I was “expecting” one of two things:

1. the definition `f(a=1,b=2)=a+2b` to throw an error about a missing `;`, or for `f(b=1,a=2)` to return `4`
2. ~~6~~ 4 defintions of `f`: 3 using positional arguments, and ~~3~~ 1 using keyword arguments and `f()` to throw an ambiguity error.

Obviously I’ve got something horribly wrong. Appreciate any help.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 13, 2019, 7:51pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/2 "2019-05-13T19:51:50Z")

</div>

> [@taqtiqa-mark](#):
>
> the definition `f(a=1,b=2)=a+2b` to throw an error about a missing `;` , or for `f(b=1,a=2)` to return `4`

That seems pretty bad, it is very useful to be able to define default values of arguments.

> [@taqtiqa-mark](#):
>
> 6 defintions of `f` : 3 using positional arguments, and 3 using keyword arguments and `f()` to throw an ambiguity error.

`f()` is overwritten by the keyword definition. Why would there be 3 methods for the keyword arguments?

---

<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: [May 13, 2019, 7:53pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/3 "2019-05-13T19:53:05Z")

</div>

I think this sould give you a hint:

> [@taqtiqa-mark](#):
>
> julia\> f(a=1,b=2)=1+2b f (generic function with 3 methods)

From my understanding, `f(a=1,b=2)=1+2b` actually creates 3 methods, one without arguments, one just with one argument `a` and one with both arguments. Keywords don’t participate in dispatch at all, so `f(;a=0, b=1) = ...` just overrides your definition for no parameters and is the one getting called by `f()`

---

<div class="post-metadata">

### Author: ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)
#### Post date: [May 13, 2019, 8:04pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/4 "2019-05-13T20:04:03Z")

</div>

> [@kristoffer.carlsson](#):
>
> That seems pretty bad, it is very useful to be able to define default values of arguments.

No argument here. So that section is only about default/optional arguments and not about keyword arguments?

> [@kristoffer.carlsson](#):
>
> Why would there be 3 methods for the keyword arguments?

Honestly I’m in the dark here, but given that in one definition `f(b=1,a=2)` throws and error and for the other it returns a value, it seemed to me that Julia recognizes `f(a=1,b=2)=...` is distinct from `f(;a=1,b=2)=...`. So I (obviously wrongly) thought maybe the same applied to the 3 definitions. Again I’m not arguing any of my understanding makes sense - I am confused.

It appears the explanation is: that documentation section deals with (positional) default values not keyword values, hence the `ERROR: function f does not accept keyword arguments` is expected?

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 13, 2019, 8:12pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/5 "2019-05-13T20:12:51Z")

</div>

> [@taqtiqa-mark](#):
>
> So that section is only about default/optional arguments and not about keyword arguments?

The last part is the thing about keyword arguments:

> Keyword arguments behave quite differently from ordinary positional arguments. In particular, they do not participate in method dispatch. Methods are dispatched based only on positional arguments, with keyword arguments processed after the matching method is identified.

---

<div class="post-metadata">

### Author: ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)
#### Post date: [May 13, 2019, 8:15pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/6 "2019-05-13T20:15:47Z")

</div>

@taqtiqa-mark Thanks for putting the question out there. I ran into something like this recently and gave up and simplified what I was doing but now realize that I got confused in a similar fashion to how you did. I think in my case I have some implicitly pythonic assumptions about how things should work that trip me up.

---

<div class="post-metadata">

### Author: ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)
#### Post date: [May 13, 2019, 8:17pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/7 "2019-05-13T20:17:08Z")

</div>

> [@simeonschaub](#):
>
> `f(;a=0, b=1) = ...` just overrides your definition for no parameters and is the one getting called by `f()`

Yes, the overriding confused me… the initial error from `f(b=1, a=2)` and then its success made me think that Julia could distinguish the two methods.

On reflection… maybe `f(b=1, a=2)` should still throw an error and `f(;b=1, a=2)` should return `8`?

---

<div class="post-metadata">

### Author: ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)
#### Post date: [May 13, 2019, 8:20pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/8 "2019-05-13T20:20:52Z")

</div>

> [@kristoffer.carlsson](#):
>
> The last part is the thing about keyword arguments:

Ack.

---

<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: [May 13, 2019, 8:26pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/9 "2019-05-13T20:26:05Z")

</div>

> [@taqtiqa-mark](#):
>
> On reflection… maybe `f(b=1, a=2)` should still throw an error and `f(;b=1, a=2)` should return `8` ?

That wouldn’t make sense to me. `f(b=1, a=2)` is a very useful shortcut to save having to create three different method definitions and should not be confused as keyword arguments. Keyword arguments are always optional and can only be specified by name. You also can’t do dispatch on them based on types. `f(;b=1, a=2)` therefore represents quite a different use case, and you should decide depending on your use case, what the most intuitive way of calling your function with these arguments is.

---

<div class="post-metadata">

### Author: ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)
#### Post date: [May 13, 2019, 8:42pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/10 "2019-05-13T20:42:00Z")

</div>

> [@simeonschaub](#):
>
> `f(b=1, a=2)` is a very useful shortcut to save having to create three different method definitions and should not be confused as keyword arguments.

Agreed, except the shortcut methods defining default values was actually defined for the _non-keyword_ method `f(a=2,b=1)`, and now `f(b=1,a=2)` quietly succeeds (instead of throwing an error) simply because elsewhere I defined a method that, by coincidence (misfortune in production), happens to use the same names for keywords.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [May 13, 2019, 8:46pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/11 "2019-05-13T20:46:30Z")

</div>

Without defining the keyword method `f(a=2, b=1)` would have failed as well.

---

<div class="post-metadata">

### Author: ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)
#### Post date: [May 13, 2019, 8:51pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/12 "2019-05-13T20:51:53Z")

</div>

💡 That did it. 😊

---

<div class="post-metadata">

### Author: ![taqtiqa-mark](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/taqtiqa-mark/32/4383_2.png) [@taqtiqa-mark](https://discourse.julialang.org/u/taqtiqa-mark)
#### Post date: [May 13, 2019, 11:03pm UTC](https://discourse.julialang.org/t/define-f-with-keyword-arguments-function-f-does-not-accept-keyword-arguments/24186/13 "2019-05-13T23:03:17Z")

</div>

**Update:**  
The following was an artifact of using the same REPL session used to define `f(a=1,b=2)`:

> [@taqtiqa-mark](#):
>
> After some digging I realized I had thought I was looking at something like this definition:
> 
> ```julia
> julia> f(; a=1,b=2)=2a+4b
> f (generic function with 3 methods)
> julia> f(b=1,a=2)
> 8
> 
> ```

In fact in `v1.0.2` using a clean REPL:

```julia
julia> f(;a=1,b=2)=2a+4b
f (generic function with 1 method)

julia> f(b=1,a=2)
8

julia> f()
10

```

For those following along at home…

@klaff and I [were](https://discourse.julialang.org/t/fun-with-kwargs-methods/15711) not [alone…](https://github.com/JuliaLang/julia/issues/9498).  
Some attempt at [documenting what the current behavior is…](https://github.com/JuliaLang/julia/pull/32020)
