I was always wondered what does “;” means in all examples .
The following functions give me the same output.
using PlotlyBase
function linescatter1()
trace1 = scatter(;x=1:4, y=[10, 15, 13, 17], mode="markers")
trace2 = scatter(;x=2:5, y=[16, 5, 11, 9], mode="lines")
trace3 = scatter(;x=1:4, y=[12, 9, 15, 12], mode="lines+markers")
Plot([trace1, trace2, trace3])
end
function linescatter2()
trace1 = scatter(x=1:4, y=[10, 15, 13, 17], mode="markers")
trace2 = scatter(x=2:5, y=[16, 5, 11, 9], mode="lines")
trace3 = scatter(x=1:4, y=[12, 9, 15, 12], mode="lines+markers")
Plot([trace1, trace2, trace3])
end
oheil
May 15, 2021, 5:05pm
2
I think it’s just function syntax for keyword parameters: Functions · The Julia Language
example:
julia> function t(; x = 1, y = 2 )
println(x,y)
end
julia> t(x=4,y=5)
45
julia> t(;x=4,y=5)
45
You can omit the ;
When the function is called, the semicolon is optional
1 Like
I see. But why should I use it? In other words, why all examples are written with “;” in function calls?
oheil
May 15, 2021, 6:21pm
4
Perhaps to make it clear that these are keyword parameter? I don’t know, why is this important?
jules
May 15, 2021, 6:34pm
5
You don’t need it for keyword arguments, but you do need it for splatting keyword arguments.
1 Like
I was just looking for a reason why to use it.
jules
May 15, 2021, 7:27pm
7
Oh and you also need it when defining functions with keyword arguments. And for these reasons some people just always add them so they don’t have to think about it
Here’s two other cases where you would need to use ;
If we define
kwargs = Dict(:x => 3, :y => 4)
foo(; x, y) = x + y # x and y are keyword arguments
Then you need to use
foo(; kwargs...) # equivalent to foo(; x=3, y=4)
Instead of
foo(kwargs...) # equivalent to foo(x => 3, y => 4)
Also with
bar = "baz"
foo(; bar) = println(bar)
Then you want
foo(; bar) # equivalent to foo(bar=bar)
Instead of
foo(bar)
Although these are fairly specific cases. In most cases you can just ignore the semicolon.
3 Likes