The Power of Functions in Kotlin

Kayvan Kaseb
Software Development
8 min readApr 19, 2024
The picture is provided by Unsplash

In Kotlin, functions are like supercharged tools. You can pass them around like any other data. This enables you to store them in variables, use them as arguments, and even return them from other functions. This flexibility keeps your code clean and reusable, making Kotlin a great language for writing functional programs. This article aims to discuss some main concepts and practices in using functions in Kotlin.

Introduction

Initially, functional programming is a programming paradigm that emphasizes treating functions as first-class citizens and encourages code that is declarative, immutable, and leverages higher-order functions. This approach focuses on “what” the code needs to achieve rather than the step-by-step instructions (imperative style). By using functions as building blocks and avoiding modifications to data, functional programming aims to create clean, concise, and testable code. In Kotlin, functions go beyond simple blocks of code to become powerful tools. This means you can pass various data types as arguments to functions, including functions themselves, collections (lists, sets, maps), and arrays. They can be treated like any other data type. This enables you to store them in variables, pass them as arguments, and even return them from other functions. This promotes code reusability and flexibility.

Passing Functions

A higher-order function is a function that takes functions as parameters, or returns a function.

In Kotlin, higher-order functions help developers write clean, expressive, and maintainable code. Also, it makes it an excellent choice for building modern and scalable applications. This allows them to:

  • Take functions as arguments: This enables you to pass functionality around as a value, promoting code reuse.
  • Return functions as results: This provides you to create functions that dynamically generate other functions.

For example, the following code defines a higher-order function called filterNumbers that takes a list of integers (numbers) and a lambda function (filterLogic) as parameters. The lambda function defines the filtering logic, which determines whether each element of the list should be included in the result.

fun filterNumbers(numbers: List<Int>, filterLogic: (Int) -> Boolean): List<Int> {
return numbers.filter(filterLogic)
}

val evenNumbers = filterNumbers(listOf(1, 2, 3, 4)) { it % 2 == 0 }

In Kotlin, lambda expressions are a concise way to define anonymous functions. They are commonly used when you require to pass functions as arguments to other functions, particularly in higher-order functions.

fun performOperation(a: Int, b: Int, operation: (Int, Int) -> Int): Int {
return operation(a, b)
}

val result = performOperation(5, 3) { x, y -> x * y }

In this example, the lambda { x, y -> x * y } multiplies its two parameters.

In the following example, processArray takes an array of integers and a function that operates on integers and returns an integer.

fun processArray(array: Array<Int>, operation: (Int) -> Int) {
val result = array.map { operation(it) }
println("Processed array: $result")
}

fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)

val addOne: (Int) -> Int = { it + 1 }
processArray(numbers, addOne)

val square: (Int) -> Int = { it * it }
processArray(numbers, square)
}

To follow up the rules of clear class structure and separation of concerns in coding, we can change it to following classes:

Define a separate class for running the code:

class ProgramRunner {
fun run() {
val myTestClass = MyTestClass()
myTestClass.processNumbers()
}
}

Then, move the logic from main() to the MyTestClass class:

class MyTestClass {
fun processNumbers() {
val numbers = arrayOf(1, 2, 3, 4, 5)

val addOne: (Int) -> Int = { it + 1 }
processArray(numbers, addOne)

val square: (Int) -> Int = { it * it }
processArray(numbers, square)
}

private fun processArray(array: Array<Int>, operation: (Int) -> Int) {
val result = array.map { operation(it) }
println("Processed array: $result")
}
}

Eventually, move the logic from main() to the MyTestClass class:

fun main() {
val programRunner = ProgramRunner()
programRunner.run()
}

Separation of concerns is a form of abstraction. As with most abstractions, separating concerns means adding additional code interfaces, generally creating more code to be executed. The extra code can result in higher computation costs in some cases, but in other cases also can lead to reuse of more optimized code.

Passing Collections

Kotlin functions can readily accept basic collections as arguments, such as sets, lists, and maps. For instance, a function called printNames that takes a list of strings (List<String>) as a parameter:


fun printNames(names: List<String>) {
for (name in names) {

println(name)
}
}

val people = listOf("Alice", "Bob", "Charlie")

printNames(people)

Besides, you can be able to use the implicit it parameter in Kotlin lambda expressions when the lambda has just only one parameter. So, you can re-write the code as follows:

fun printNames(names: List<String>) {
names.forEach { println(it) }
}

So, forEach is a function of the List class that iterates over each element of the list and applies the lambda function to it. The lambda function println(it) takes a single parameter it, which represents each element of the list in turn. This can make the code more concise and readable, especially when the lambda is simple and the parameter name is not important for understanding the logic.

In addition, in the following example, this function takes a map of usernames and names, updates the name associated with a specific username, and returns the modified map. The original map remains unchanged due to the creation of a mutable copy at the beginning of the function

fun updateUsername(users: MutableMap<String, String>, username: String, newName: String): MutableMap<String, String> {
users[username] = newName

return users
}

fun main() {
val users = mutableMapOf("user1" to "Alice", "user2" to "Bob", "user3" to "Charlie")
val usernameToUpdate = "user2"
val newName = "Bobby"

println("Original Users: $users")
val updatedUsers = updateUsername(users, usernameToUpdate, newName)
println("Updated Users: $updatedUsers")
}

Output:

Original Users: {user1=Alice, user2=Bob, user3=Charlie}
Updated Users: {user1=Alice, user2=Bobby, user3=Charlie}

Read-only and Mutable Collections

When passing collections (lists, sets, maps) as arguments to functions in Kotlin, it is significant to choose the appropriate collection type based on whether the function needs to modify the collection itself. Read-only Collections (listOf(), setOf(), mapOf()):

  • Use these for collections that the function will only iterate over or access elements without modification. They are immutable, this means their contents cannot be changed after creation.

For example:

fun printNames(names: List<String>) { 
for (name in names) {
println(name)
}
}

val people = listOf("Alice", "Bob", "Charlie")
printNames(people)

Mutable Collections (mutableListOf(), mutableSetOf(), mutableMapOf()):

  • Use these when the function needs to modify the collection contents (adding, removing, or updating elements). It can potentially lead to unexpected behavior if not managed carefully.

Lambda Captures

Lambda captures are a powerful feature that allows lambda functions to access variables from the scope in which they are defined, even after the surrounding function has returned. This makes lambdas very versatile and can be used in a variety of situations. For instance, capturing a variable and modifying it within the lambda:

fun createCounter(): () -> Int {
var count = 0

return {
count++ // Modifying captured count
}
}

val counter = createCounter()

println(counter())
println(counter())
println(counter())

Benefits:

Lambdas with captures promote concise and functional code.

They enhance code readability by keeping logic close to where it’s used.

They allow for creating stateful functions when needed.

Default Function Arguments

Default function arguments are a powerful feature in Kotlin that allow you to define optional parameters for your functions. This offers more flexibility when calling the function and improves code readability. When defining a function parameter, you can specify a default value after the parameter name and an equal sign (=).

fun greet(name: String = "World") {
println("Hello, $name!")
}

Then, you can call the function with or without providing a value for the argument with a default value. If you do not provide a value, the default value will be used.

fun main() {
greet() // Output: Hello, World! (Uses default name
greet("Alice") // Output: Hello, Alice! (Provides custom name)
greet(salutation = "Hi")
}

Benefits:

Setting optional configuration values for functions.

Providing fallback values for missing data.

Creating functions with common behavior and variations based on arguments.

Destructuring Collections

Destructuring collections is a powerful feature in Kotlin that allows you to unpack and access elements of collections. In other words, Destructuring collections makes your code more readable and expressive because you can use meaningful variable names instead of accessing elements by their positional index. In particular, this is helpful when working with complex data structures or passing multiple values together as a single parameter. For example:

fun greet(name: String, age: Int) {
println("Hello, $name! You are $age years old.")
}

val person = listOf("Alice", 30)
greet(person[0], person[1]) // Traditional way

// Destructuring!
greet(person.first(), person.last()) // More readable

Benefits:

Extracting specific data from collections passed as function arguments.

Assigning meaningful names to elements for better code clarity.

Simplifying loops that iterate through collections and access elements.

Function Types

In Kotlin, functions are considered first-class constructs. This enables you to declare variables that hold references to functions.

fun add(x: Int, y: Int): Int {
return x + y
}

// Declare a variable of function type
val sumFunction: (Int, Int) -> Int = add

val result = sumFunction(5, 3) // Call the function using the variable
println(result) // Output: 8

Benefits:

This allows you to define functions that operate on other functions. This is a core concept in functional programming and enables powerful abstractions.

You can easily create reusable and adaptable code by working with functions as data.

Using clear function names can improve code readability and maintainability

Function-as-Result in Kotlin

As mentioned earlier, Kotlin has ability to return functions as results. This opens doors for creating functions that dynamically generate other functions based on specific criteria. The returned function can be set to specific needs based on the arguments passed to the original function. This is a typical pattern used in functional programming to create functions dynamically or to configure behavior based on certain conditions. For instance:


fun createMultiplier(factor: Int): (Int) -> Int {

return { number -> number * factor }
}

fun main() {
// Create a multiplier function that doubles its input
val double = createMultiplier(2)

// Create a multiplier function that triples its input
val triple = createMultiplier(3)

// Use the double multiplier function to double a value
val doubledValue = double(5) // Should return 10

// Use the triple multiplier function to triple a value
val tripledValue = triple(7) // Should return 21

// Print the results
println("Doubled Value: $doubledValue")
println("Tripled Value: $tripledValue")
}

Benefits:

This avoid writing repetitive code for similar operations with different parameters(Code Reuse).

This package related logic within a function that generates specialized functions(Encapsulation).

Other considerations

Extension Functions (Supercharge Existing Classes): They add functionality to existing classes without modifying the original code. This keeps your codebase organized and avoids cluttering core classes.

Data Classes (Less Code, More Fun!) They can simplify data handling with built-in features like equals(), hashCode(), and toString().

Inline Functions: Speed Demons (But Ride with Caution!): Kotlin’s inline functions provide a compelling way to boost performance for small, frequently called functions.

In Conclusion

In Kotlin, functions go beyond simple blocks of code to become powerful tools. They can be treated like any other data type. This enables you to store them in variables, pass them as arguments, and even return them from other functions. This flexibility, combined with features like default arguments and Destructuring, empowers you to write concise and expressive code. Functions can be used for various purposes, including defining reusable logic, implementing callbacks and event listeners, and creating custom sorting criteria. By leveraging functions efficiently, you can significantly improve the readability, maintainability, and overall your Kotlin code quality.

--

--

Kayvan Kaseb
Software Development

Senior Android Developer, Technical Writer, Researcher, Artist, Founder of PURE SOFTWARE YAZILIM LİMİTED ŞİRKETİ https://www.linkedin.com/in/kayvan-kaseb