Some times a function can be defined
with more arguments, in that case it is very difficult to remember the order of
arguments. Julia provides a facility that you can refer arguments by name.
Semicolon is used in signature of the function to differentiate keyword arguments.
function
processData(x,y;longitude=10.0,latitude=20.0, color="Black")
All the arguments after semi colon are
keyword arguments, these can be referred by name.
You can call above function in following
ways.
processData(10,20)
processData(10,20,color="Red")
processData(10,20,color="Red",
latitude=123.4466)
julia> function processData(x,y;longitude=10.0,latitude=20.0, color="Black") println("x=$x") println("y=$y") println("longitude=$longitude") println("latitude=$latitude") println("color=$color") end processData (generic function with 1 method) julia> processData(10,20) x=10 y=20 longitude=10.0 latitude=20.0 color=Black julia> processData(10,20,color="Red") x=10 y=20 longitude=10.0 latitude=20.0 color=Red julia> processData(10,20,color="Red", latitude=123.4466) x=10 y=20 longitude=10.0 latitude=123.4466 color=Red
Keyword
arguments and varargs
By using varargs (…), you can collect
extra keyword arguments.
function
processData(x,y;color="Black", args...)
All the extra keyword arguments are
collected in the argument args, args will be a collection of (key,value) tuples.
julia> function processData(x,y;color="Black", args...) println("x=$x") println("y=$y") println("color=$color") for i in args println(i) end end processData (generic function with 1 method) julia> processData(10,20,color="Red", latitude=123.4466) x=10 y=20 color=Red (:latitude,123.4466) julia> processData(10,20,color="Red", latitude=123.4466, name="Krishna", area="Bangalore") x=10 y=20 color=Red (:latitude,123.4466) (:name,"Krishna") (:area,"Bangalore")
No comments:
Post a Comment