- Kotlin Programming By Example
- Iyanu Adelekan
- 136字
- 2025-04-04 17:16:16
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 templates:
"${firstName} ${surname}"
The values for first name and last name are interpolated into the string of characters.