Functions in Kotlin are declared with fun:
fun greet(name: String): String {
return "Hello, $name!"
}
// Single-expression function — the { return ... } can be shortened:
fun greetShort(name: String) = "Hello, $name!"
Default and named parameters
fun createUser(name: String, age: Int = 18, isAdmin: Boolean = false) {
println("$name, age $age, admin=$isAdmin")
}
createUser("Ada") // age defaults to 18
createUser("Grace", age = 32) // named argument
createUser(name = "Linus", isAdmin = true) // skip age entirely, using its default
Default parameters remove the need for multiple overloaded versions of the same function — a pattern you’ll see constantly in Android SDK code and in libraries like Retrofit later in this series.
- Image
- SKU
- Rating
- Price
- Stock
- Availability
- Add to cart
- Description
- Content
- Weight
- Dimensions
- Additional information