Functions in Kotlin Explained
Functions are the building blocks of any Kotlin program. They help organize code, promote reusability, and make your code more maintainable. Let’s explore the different ways to work with functions in Kotlin.
Basic Function Declaration
// Simple function
fun greet(name: String): String {
return "Hello, $name!"
}
// Single-expression function
fun square(x: Int) = x * x
// Function with default parameters
fun greet(name: String, greeting: String = "Hello") = "$greeting, $name!"
Function Parameters
Named Parameters
fun createUser(name: String, age: Int, email: String) {
// Function body
}
// Call with named parameters
createUser(
name = "John",
age = 25,
email = "john@example.com"
)
Variable Number of Arguments
fun sum(vararg numbers: Int): Int {
return numbers.sum()
}
// Usage
sum(1, 2, 3, 4, 5)
Function Types
Basic Function Types
// Function type declaration
val operation: (Int, Int) -> Int = { a, b -> a + b }
// Function as parameter
fun calculate(x: Int, y: Int, operation: (Int, Int) -> Int): Int {
return operation(x, y)
}
Higher-Order Functions
fun processList(
list: List<Int>,
operation: (Int) -> Int
): List<Int> {
return list.map(operation)
}
// Usage
val numbers = listOf(1, 2, 3, 4, 5)
val doubled = processList(numbers) { it * 2 }
Extension Functions
// Add function to String class
fun String.addExclamation() = "$this!"
// Usage
val greeting = "Hello".addExclamation() // "Hello!"
Infix Functions
infix fun Int.times(str: String) = str.repeat(this)
// Usage
val result = 3 times "Hello" // "HelloHelloHello"
Local Functions
fun validateUser(user: User): Boolean {
// Local function
fun validateName(name: String): Boolean {
return name.length >= 3
}
return validateName(user.name)
}
Function Scope
Top-level Functions
// Can be called from anywhere
fun globalFunction() {
// Function body
}
Member Functions
class Calculator {
fun add(a: Int, b: Int) = a + b
}
Function Modifiers
Visibility Modifiers
private fun privateFunction() { }
protected fun protectedFunction() { }
internal fun internalFunction() { }
public fun publicFunction() { }
Other Modifiers
inline fun measureTime(block: () -> Unit) {
val start = System.currentTimeMillis()
block()
val end = System.currentTimeMillis()
println("Time taken: ${end - start}ms")
}
Best Practices
-
Keep functions small and focused
// Good fun calculateTotal(items: List<Item>): Double { return items.sumOf { it.price } } // Avoid fun processOrder(order: Order) { // Too many responsibilities }
-
Use meaningful names
// Good fun calculateAverage(numbers: List<Int>) // Avoid fun calc(nums: List<Int>)
-
Leverage default parameters
// Good fun createUser( name: String, age: Int = 0, email: String? = null )
Conclusion
Functions in Kotlin are powerful and flexible. They help you:
- Write clean, maintainable code
- Promote code reuse
- Make your code more testable
- Create expressive APIs
Stay tuned for our next post where we’ll explore Kotlin’s null safety features!