Function Definition in Kotlin

Level 3106 keystrokes

Defines a function with a typed parameter and a return type, then calls it.

Kotlin code

// fun defines a function; name: String is a parameter of type String
// The : String after the parentheses is the return type
1fun greet(name: String): String {
// Build the greeting with a string template and return it
2 return "Hello, $name!"
3}
4
// Call greet with "Kotlin"; message is "Hello, Kotlin!"
5val message = greet("Kotlin")
// Prints Hello, Kotlin!
6println(message)

Output

Hello, Kotlin!
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Kotlin code and how often they appear.

  • "× 4
  • (× 3
  • )× 3
  • :× 2
  • ,× 1
  • !× 1
  • {× 1
  • }× 1
  • =× 1
  • $× 1

More Kotlin examples at level 3

← Kotlin typing practice