# A keyword argument must have a default value?

**URL:** <https://discourse.julialang.org/t/a-keyword-argument-must-have-a-default-value/108421>\
**Category:** General Usage\
**Tags:** question, keyword-arguments\
**Created:** [January 6, 2024, 3:20am UTC](https://discourse.julialang.org/t/a-keyword-argument-must-have-a-default-value/108421 "2024-01-06T03:20:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 6, 2024, 3:20am UTC](https://discourse.julialang.org/t/a-keyword-argument-must-have-a-default-value/108421/1 "2024-01-06T03:20:01Z")

</div>

This seems a common practice in the codes I saw.

Is it required or not?

---

<div class="post-metadata">

**Author:** ![jiang\_ming\_zhang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jiang_ming_zhang/32/204063_2.png) [@jiang\_ming\_zhang](https://discourse.julialang.org/u/jiang_ming_zhang)\
**Post date:** [January 6, 2024, 3:21am UTC](https://discourse.julialang.org/t/a-keyword-argument-must-have-a-default-value/108421/2 "2024-01-06T03:21:40Z")

</div>

With a small test, I see it is not required

```julia
julia> function f2(x; year )
       return x^2 + year
       end
f2 (generic function with 1 method)

julia> f2(10; 20)
ERROR: syntax: invalid keyword argument syntax "20" around REPL[9]:1
Stacktrace:
 [1] top-level scope
   @ REPL[9]:1

julia> f2(10; year = 20)
120

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [January 6, 2024, 5:12am UTC](https://discourse.julialang.org/t/a-keyword-argument-must-have-a-default-value/108421/3 "2024-01-06T05:12:00Z")

</div>

FYI, the [official manual](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments) is quite clear about most of the basic stuff. I strongly encourage you to look there for an answer to basic questions about the language and if you don’t understand the manual come here and post a question. Ideally that also leads to an improvement of the manual to make it even clearer over time.

The chapter overview on the left might even let you discover new things, that you would not have thought of 🙂 So have a look and see what Julia offers you!  
[https://docs.julialang.org/en/v1/manual/getting-started/](https://docs.julialang.org/en/v1/manual/getting-started/)

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [January 6, 2024, 5:25am UTC](https://discourse.julialang.org/t/a-keyword-argument-must-have-a-default-value/108421/4 "2024-01-06T05:25:38Z")

</div>

> [@jiang\_ming\_zhang](#):
>
> I see it is not required

Right. If you do not provide a default value for a keyword argument when you define the function, then a value will need to be provided when the function is called:

```julia-repl
julia> foo(x; y) = x + y
foo (generic function with 1 method)

julia> foo(1)
ERROR: UndefKeywordError: keyword argument `y` not assigned
Stacktrace:
 [1] foo(x::Int64)
   @ Main ./REPL[1]:1
 [2] top-level scope
   @ REPL[2]:1

julia> foo(1; y=2)
3

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 6, 2024, 8:44am UTC](https://discourse.julialang.org/t/a-keyword-argument-must-have-a-default-value/108421/5 "2024-01-06T08:44:29Z")

</div>

I had read somewhere that (in some context) `;x` was translated by the parser(?) into `;x=x`… in this case, it doesn’t seem like it

```julia
julia> f(x;y=y)=x+y
f (generic function with 1 method)

julia> y=2
2

julia> f(1)
3

julia> g(x;z)=x+z
g (generic function with 1 method)

julia> g(1)
ERROR: UndefKeywordError: keyword argument `z` not assigned
Stacktrace:
 [1] g(x::Int64)
   @ Main c:\Users\sprmn\.julia\environments\v1.9.0\array11.jl:137
 [2] top-level scope
   @ c:\Users\sprmn\.julia\environments\v1.9.0\array11.jl:139

julia> z=2
2

julia> g(1)
ERROR: UndefKeywordError: keyword argument `z` not assigned
Stacktrace:

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [January 6, 2024, 9:43am UTC](https://discourse.julialang.org/t/a-keyword-argument-must-have-a-default-value/108421/7 "2024-01-06T09:43:19Z")

</div>

From the [manual linked above](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments)

> When a bare identifier or dot expression occurs after a semicolon, the keyword argument name is implied by the identifier or field name. For example `plot(x, y; width)` is equivalent to `plot(x, y; width=width)` and `plot(x, y; options.width)` is equivalent to `plot(x, y; width=options.width)`.

This isn’t super explicit about it, but the syntactic sugar is only described in the context of calling a function with keywords - not defining it.

---

<div class="post-metadata">

**Author:** ![dmolina](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmolina/32/5246_2.png) [@dmolina](https://discourse.julialang.org/u/dmolina)\
**Post date:** [January 6, 2024, 11:41am UTC](https://discourse.julialang.org/t/a-keyword-argument-must-have-a-default-value/108421/8 "2024-01-06T11:41:24Z")

</div>

As you have seen, it is not required but recommended, I usually always use default in keywork parameters.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [January 6, 2024, 2:55pm UTC](https://discourse.julialang.org/t/a-keyword-argument-must-have-a-default-value/108421/9 "2024-01-06T14:55:30Z")

</div>

another point to specify better would be the following:

> The nature of keyword arguments makes it possible to specify the same argument more than once. For example, in the call `plot(x, y; options..., width=2)` it is possible that the `options` structure also contains a value for `width`. In such a case the rightmost occurrence takes precedence; in this example, `width` is certain to have the value `2`.

in case a parameter depends on another that precedes it, this “precedence rule” is overridden

```julia

f(x;y1=1,y2=2,yy1=y1+3,y3=3,y4=4)=x+y1+y2+yy1+y3+y4

f(1) #15

f(1, y3=0,y1=0) #10

```
