Default and Named Arguments
Overloading of functions is common in Java and is the way to create functions that can take different number and type of arguments. That’s an option in Kotlin as well, but the feature of default arguments is a simpler and better way to evolve functions, though it requires recompilation as it breaks binary compatibility. Also, named arguments is a great way to create readable code. We’ll focus on those two features now.
Evolving Functions with Default Arguments
Quickly glance at the greet() function we wrote earlier, repeated here for convenience.
| fun greet(name: String): String = "Hello $name" |
| |
| println(greet("Eve")) //Hello Eve |
The greet() function has a hardcoded string "Hello", but what if we want to ...
Get Programming Kotlin now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.