The following snippet illustrates several interesting features available in Kotlin:
fun formatName(name: String?) = name ?: "Fellow Human"
fun greetReader(greeting: String = "Hey", name: String?) =
println("$greeting ${formatName(name)}")
fun main(args: Array<String>) {
greetReader("Hello!", "Reader")
// Hello! Reader
}
As you look at these few lines of code, you will notice a few items of interest, which are as follows:
- The use of the fun keyword to define a new function
- Functions that exist outside of any enclosing class
- Demonstrations of null and non-null types
- Support for default parameter values and String templates