Functions

In Kotlin, functions are declared using the fun keyword. The following code snippet shows an example:

    fun helloBasic(name: String): String {      return "Hello, $name!"    }

Function arguments are specified in brackets after the function name. name is an argument of type String. The function return type is specified after the arguments. The return type of the function is String.

The following line of code shows the invocation of the helloBasic function:

    println(helloBasic("foo")) // => Hello, foo!

Kotlin also allows n. The following line of code shows an example:

    println(helloBasic(name = "bar"))

Function arguments can optionally have a defa

    fun helloWithDefaultValue(name: String = "World"): String {      return "Hello, $name!"    }

The following ...

Get Mastering Spring 5.0 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.