Thursday, August 17, 2017

Julia - Language - Functions - Optional Arguments

Functions in Julia can have optional input parameters. The optional input parameters are placed in the last. To make a parameter optional we provide a default value to it.

$ 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 multiplyNos(a,b,c=1)
       prod = a*b*c
       println("we have the values of a = $a ; b= $b; c= $c")
       println("product of numbers = $prod \n")
       end
multiplyNos (generic function with 2 methods)


In this example, c is a optional argument. This is beacause it is having a default value of 1. That means, In case the third value is not provided - then it is considered as 1.


julia> multiplyNos(5,6)
we have the values of a = 5 ; b= 6; c= 1
product of numbers = 30 

Now when I pass the third value as parameter. It overrides the default value. 

julia> multiplyNos(5,6,2)
we have the values of a = 5 ; b= 6; c= 2
product of numbers = 60 

julia> 

No comments:

Post a Comment