What if you could teach your functions new tricks without rewriting them every time?
Why Passing lambdas to functions in Kotlin? - Purpose & Use Cases
Imagine you want to perform different actions on a list of numbers, like doubling them or checking if they are even. Without lambdas, you'd have to write separate functions for each action and call them manually every time.
This manual way is slow and repetitive. You end up writing lots of similar code, which is easy to mess up and hard to change later. It feels like doing the same chore over and over without any shortcuts.
Passing lambdas to functions lets you send little pieces of code as arguments. This means you can write one flexible function and tell it exactly what to do each time, without rewriting the whole thing.
fun double(numbers: List<Int>): List<Int> {
val result = mutableListOf<Int>()
for (n in numbers) {
result.add(n * 2)
}
return result
}
fun even(numbers: List<Int>): List<Int> {
val result = mutableListOf<Int>()
for (n in numbers) {
if (n % 2 == 0) result.add(n)
}
return result
}fun process(numbers: List<Int>, action: (Int) -> Unit) {
for (n in numbers) {
action(n)
}
}
process(listOf(1,2,3)) { println(it * 2) }
process(listOf(1,2,3)) { if (it % 2 == 0) println(it) }You can write one powerful function that adapts to many tasks by simply passing different lambdas, making your code cleaner and easier to maintain.
Think about a music app that plays songs differently based on user mood. Instead of writing separate play functions, it can accept a lambda to customize how each song plays, like adding effects or skipping tracks.
Manual repetition is slow and error-prone.
Lambdas let you pass custom actions into functions.
This makes code flexible, reusable, and easier to change.
Practice
Solution
Step 1: Understand what a lambda is
A lambda is a small piece of code that you can write inline and pass around like a value.Step 2: Recognize its use in functions
Functions can accept lambdas to customize behavior without changing the function code.Final Answer:
A small block of code that can be passed and executed inside the function -> Option AQuick Check:
Lambda = small code block passed to function [OK]
- Thinking lambda is a variable type
- Confusing lambda with loops
- Believing lambda is a keyword for classes
Solution
Step 1: Check function parameter type
The function expects a lambda with no parameters and no return value: () -> Unit.Step 2: Check how lambda is called and passed
Inside the function, calling action() executes the lambda. Passing greet { println("Hi") } is correct syntax.Final Answer:
fun greet(action: () -> Unit) { action() } greet { println("Hi") } -> Option BQuick Check:
Correct lambda syntax = fun greet(action: () -> Unit) { action() } greet { println("Hi") } [OK]
- Wrong lambda type syntax
- Forgetting to call lambda with ()
- Passing lambda as function argument incorrectly
fun operate(x: Int, action: (Int) -> Int): Int {
return action(x)
}
fun main() {
val result = operate(5) { it * 2 }
println(result)
}Solution
Step 1: Understand the operate function
It takes an integer and a lambda that doubles the integer (it * 2).Step 2: Calculate the result
Passing 5 to the lambda doubles it to 10, which is returned and printed.Final Answer:
10 -> Option CQuick Check:
5 * 2 = 10 [OK]
- Confusing input and output values
- Forgetting lambda parameter 'it'
- Assuming no return from lambda
fun applyOperation(x: Int, op: (Int) -> Int) {
op(x)
}
fun main() {
applyOperation(3) { println(it * 3) }
}Solution
Step 1: Check function signature and lambda
applyOperation expects a lambda returning Int, but the lambda only prints and returns Unit.Step 2: Understand mismatch consequences
Because the lambda returns Unit, but function expects Int, this causes a type mismatch error.Final Answer:
The lambda passed does not return a value but function expects Int return -> Option DQuick Check:
Lambda return type mismatch = The lambda passed does not return a value but function expects Int return [OK]
- Ignoring return type mismatch
- Thinking println returns Int
- Assuming no return needed for lambdas
filterAndTransform that takes a list of integers and two lambdas: one to filter the list and one to transform each filtered item. Which of these function signatures correctly allows passing both lambdas?Solution
Step 1: Identify correct lambda types for filtering and transforming
Filtering needs a lambda that takes Int and returns Boolean; transforming needs a lambda that takes Int and returns Int.Step 2: Match function signature with these lambda types
fun filterAndTransform(list: List<Int>, filter: (Int) -> Boolean, transform: (Int) -> Int): List<Int> matches these types correctly; others have wrong parameter or return types.Final Answer:
fun filterAndTransform(list: List<Int>, filter: (Int) -> Boolean, transform: (Int) -> Int): List<Int> -> Option AQuick Check:
Filter returns Boolean, transform returns Int [OK]
- Using wrong lambda parameter or return types
- Confusing filter and transform lambda roles
- Using lambdas with no parameters
Ready to prove it?
Pass the test and this pattern moves unknown
