Return values

A return value—as the name implies—is the value that a method returns. Functions in Kotlin can return values upon execution. The type of the value returned by a function is defined by the function's return type. This is demonstrated in the following code snippet:

fun returnFullName(firstName: String, surname: String): String {  return "${firstName} ${surname}"}fun main(args: Array<String>) {  val fullName: String = returnFullName("James", "Cameron")  println(fullName) // prints: James Cameron}

In the preceding code, the returnFullName function takes two distinct strings as its input parameters and returns a string value when called. The return type has been defined in the function header. The string returned is created via string ...

Get Kotlin Programming By Example 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.