January 2018
Intermediate to advanced
434 pages
14h 1m
English
In Kotlin, parameters of functions can have default values, and they are used when the corresponding argument is omitted. This, in turn, reduces the number of overloads. The preceding example with three different types of parameters can be resolved easily in Kotlin with a lot less code:
fun main(args: Array<String>) { foo() foo(1) foo(1,0.1) foo(1,0.1,"custom string")}fun foo(a:Int=0, b: Double =0.0, c:String="some default value"){ println("a=$a , b=$b ,c = $c")}
If you run the preceding code, you will see the following output:
Output:a=0 , b=0.0 ,c = some default valuea=1 , b=0.0 ,c = some default valuea=1 , b=0.1 ,c = some default valuea=1 , b=0.1 ...
Read now
Unlock full access