A keyword argument must have a default value?

This seems a common practice in the codes I saw.

Is it required or not?

With a small test, I see it is not required

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
1 Like

FYI, the official manual 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 :slight_smile: So have a look and see what Julia offers you!
https://docs.julialang.org/en/v1/manual/getting-started/

4 Likes

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> 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
3 Likes

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> 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:

From the manual linked above

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.

1 Like

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

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


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
1 Like