Wednesday 2 March 2016

Julia: Passing default values to arguments

In Julia you can provide default values to the arguments in function.
For example,
julia> function power(base=2,power=2)
           base^power
       end
power (generic function with 3 methods)

julia> power()
4

julia> power(5)
25

julia> power(5, 3)
125

function power(base=2,power=2)
If you call power function like power(), then it takes base=2 and power=2.

If you call power function like power(10), then it calculates 10^2

If you call power function like power(10,3), then it calculates 10^3.




Previous                                                 Next                                                 Home

No comments:

Post a Comment