Let us learn to use keyword arguments in functions of Julia Language.
We can create function with many arguments. The problem we may face is, we might forget the order in which the arguments must be specified, when calling the function and passing values to it. To solve this problem - the semi-colon (;) can be used (usually after ordering arguments ). Let us take a look:
$ julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: https://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.6.0 (2017-06-19 13:05 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu
julia> function func2(a,b,c=100;p=100,q="red")
print("The first ordered argument value is $(a). ","\n")
print("The second ordered argument value is $(b). ","\n")
print("The third ordered argument value is optional $(c). ","\n")
print("If you see a value of 100 here, you either passed a value of 100 or omitted it. $(c)","\n")
print("Let us see what happened to the keyword p: $(p)","\n")
print("Let us see what happened to the keyword q: $(q)","\n")
print("Oh yes, Let us also return something useful, like multiplying $(a) and $(b), yielding:","\n")
return a*b
end
func2 (generic function with 2 methods)
Let us pass only the values for the arguments a and b.
julia> func2(3,4)
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 100.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 100
Let us see what happened to the keyword p: 100
Let us see what happened to the keyword q: red
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
Now, we pass value for the third ordered argument.
julia> func2(3,4,5)
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 5.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 5
Let us see what happened to the keyword p: 100
Let us see what happened to the keyword q: red
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
We pass the value for one keyword argument.
julia> func2(3,4,p = pi)
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 100.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 100
Let us see what happened to the keyword p: �� = 3.1415926535897...
Let us see what happened to the keyword q: red
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
We pass the value for one keyword argument.
julia> func2(3,4,q = "hello!")
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 100.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 100
Let us see what happened to the keyword p: 100
Let us see what happened to the keyword q: hello!
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
We pass the value for another one keyword argument and interchange the order of keyword arguments.
julia> func2(3,4,5,q="it works!",p=exp(1))
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 5.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 5
Let us see what happened to the keyword p: 2.718281828459045
Let us see what happened to the keyword q: it works!
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
The below example, will really clear our concept. We placed q in the beginning. and the value of c is last.
julia> func2(q = "Bananas!",3,4,p=sqrt(3),2)
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 2.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 2
Let us see what happened to the keyword p: 1.7320508075688772
Let us see what happened to the keyword q: Bananas!
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
Let us try to pass value to the keyword argument without a keyword. It fails! That means keyword argument needs a keyword.
julia> func2(1,2,3,4,5)
ERROR: MethodError: no method matching func2(::Int64, ::Int64, ::Int64, ::Int64, ::Int64)
Closest candidates are:
func2(::Any, ::Any, ::Any; p, q) at REPL[1]:2
func2(::Any, ::Any) at REPL[1]:2
julia> func2(1,2,3,4)
ERROR: MethodError: no method matching func2(::Int64, ::Int64, ::Int64, ::Int64)
Closest candidates are:
func2(::Any, ::Any, ::Any; p, q) at REPL[1]:2
func2(::Any, ::Any) at REPL[1]:2
julia> func2(1,2,3,p=4,5)
ERROR: MethodError: no method matching func2(::Int64, ::Int64, ::Int64, ::Int64; p=4)
Closest candidates are:
func2(::Any, ::Any, ::Any; p, q) at REPL[1]:2
func2(::Any, ::Any) at REPL[1]:2 got unsupported keyword argument "p"
Now back to using keywords:
julia> func2(1,2,3,p=4)
The first ordered argument value is 1.
The second ordered argument value is 2.
The third ordered argument value is optional 3.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 3
Let us see what happened to the keyword p: 4
Let us see what happened to the keyword q: red
Oh yes, Let us also return something useful, like multiplying 1 and 2, yielding:
2
julia> func2(1,2,3,p=4,q=5)
The first ordered argument value is 1.
The second ordered argument value is 2.
The third ordered argument value is optional 3.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 3
Let us see what happened to the keyword p: 4
Let us see what happened to the keyword q: 5
Oh yes, Let us also return something useful, like multiplying 1 and 2, yielding:
2
julia>
We can create function with many arguments. The problem we may face is, we might forget the order in which the arguments must be specified, when calling the function and passing values to it. To solve this problem - the semi-colon (;) can be used (usually after ordering arguments ). Let us take a look:
$ julia
_
_ _ _(_)_ | A fresh approach to technical computing
(_) | (_) (_) | Documentation: https://docs.julialang.org
_ _ _| |_ __ _ | Type "?help" for help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 0.6.0 (2017-06-19 13:05 UTC)
_/ |\__'_|_|_|\__'_| | Official http://julialang.org/ release
|__/ | x86_64-pc-linux-gnu
julia> function func2(a,b,c=100;p=100,q="red")
print("The first ordered argument value is $(a). ","\n")
print("The second ordered argument value is $(b). ","\n")
print("The third ordered argument value is optional $(c). ","\n")
print("If you see a value of 100 here, you either passed a value of 100 or omitted it. $(c)","\n")
print("Let us see what happened to the keyword p: $(p)","\n")
print("Let us see what happened to the keyword q: $(q)","\n")
print("Oh yes, Let us also return something useful, like multiplying $(a) and $(b), yielding:","\n")
return a*b
end
func2 (generic function with 2 methods)
Let us pass only the values for the arguments a and b.
julia> func2(3,4)
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 100.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 100
Let us see what happened to the keyword p: 100
Let us see what happened to the keyword q: red
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
Now, we pass value for the third ordered argument.
julia> func2(3,4,5)
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 5.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 5
Let us see what happened to the keyword p: 100
Let us see what happened to the keyword q: red
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
We pass the value for one keyword argument.
julia> func2(3,4,p = pi)
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 100.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 100
Let us see what happened to the keyword p: �� = 3.1415926535897...
Let us see what happened to the keyword q: red
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
We pass the value for one keyword argument.
julia> func2(3,4,q = "hello!")
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 100.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 100
Let us see what happened to the keyword p: 100
Let us see what happened to the keyword q: hello!
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
We pass the value for another one keyword argument and interchange the order of keyword arguments.
julia> func2(3,4,5,q="it works!",p=exp(1))
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 5.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 5
Let us see what happened to the keyword p: 2.718281828459045
Let us see what happened to the keyword q: it works!
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
The below example, will really clear our concept. We placed q in the beginning. and the value of c is last.
julia> func2(q = "Bananas!",3,4,p=sqrt(3),2)
The first ordered argument value is 3.
The second ordered argument value is 4.
The third ordered argument value is optional 2.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 2
Let us see what happened to the keyword p: 1.7320508075688772
Let us see what happened to the keyword q: Bananas!
Oh yes, Let us also return something useful, like multiplying 3 and 4, yielding:
12
Let us try to pass value to the keyword argument without a keyword. It fails! That means keyword argument needs a keyword.
julia> func2(1,2,3,4,5)
ERROR: MethodError: no method matching func2(::Int64, ::Int64, ::Int64, ::Int64, ::Int64)
Closest candidates are:
func2(::Any, ::Any, ::Any; p, q) at REPL[1]:2
func2(::Any, ::Any) at REPL[1]:2
julia> func2(1,2,3,4)
ERROR: MethodError: no method matching func2(::Int64, ::Int64, ::Int64, ::Int64)
Closest candidates are:
func2(::Any, ::Any, ::Any; p, q) at REPL[1]:2
func2(::Any, ::Any) at REPL[1]:2
julia> func2(1,2,3,p=4,5)
ERROR: MethodError: no method matching func2(::Int64, ::Int64, ::Int64, ::Int64; p=4)
Closest candidates are:
func2(::Any, ::Any, ::Any; p, q) at REPL[1]:2
func2(::Any, ::Any) at REPL[1]:2 got unsupported keyword argument "p"
Now back to using keywords:
julia> func2(1,2,3,p=4)
The first ordered argument value is 1.
The second ordered argument value is 2.
The third ordered argument value is optional 3.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 3
Let us see what happened to the keyword p: 4
Let us see what happened to the keyword q: red
Oh yes, Let us also return something useful, like multiplying 1 and 2, yielding:
2
julia> func2(1,2,3,p=4,q=5)
The first ordered argument value is 1.
The second ordered argument value is 2.
The third ordered argument value is optional 3.
If you see a value of 100 here, you either passed a value of 100 or omitted it. 3
Let us see what happened to the keyword p: 4
Let us see what happened to the keyword q: 5
Oh yes, Let us also return something useful, like multiplying 1 and 2, yielding:
2
julia>
No comments:
Post a Comment