Passing Lambdas to Functions
Pass lambdas as arguments and use trailing lambda syntax.
Passing Lambdas to Functions is a free Kotlin Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.
Functions That Accept Lambdas
Declaring a Higher-Order Function
fun applyTwice(n: Int, op: (Int) -> Int): Int {
return op(op(n))
}
println(applyTwice(3, { it * 2 })) // 12Trailing Lambda Syntax
fun applyTwice(n: Int, op: (Int) -> Int) = op(op(n))
// Standard:
applyTwice(3, { it * 2 })
// Trailing lambda:
applyTwice(3) { it * 2 }Trailing Lambda Without Parens
fun execute(block: () -> Unit) = block()
// With parens:
execute({ println("Hello") })
// Without parens (idiomatic):
execute { println("Hello") }Passing a Lambda Variable
val double = { n: Int -> n * 2 }
fun applyTwice(n: Int, op: (Int) -> Int) = op(op(n))
println(applyTwice(5, double)) // 20Function References with ::
fun triple(n: Int) = n * 3
fun applyTwice(n: Int, op: (Int) -> Int) = op(op(n))
println(applyTwice(2, ::triple)) // 18Multiple Lambda Parameters
fun transform(
value: Int,
before: (Int) -> Int,
after: (Int) -> Int
) = after(before(value))
println(transform(5, { it * 2 }, { it + 1 })) // 11Passing a Lambda to filter
val numbers = listOf(1, 2, 3, 4, 5, 6)
val evens = numbers.filter { it % 2 == 0 }
println(evens) // [2, 4, 6]Chaining Higher-Order Functions
val result = (1..10)
.filter { it % 2 != 0 } // odds
.map { it * it } // square
.take(3) // first 3
println(result) // [1, 9, 25]Lambda Capturing Variables
var total = 0
listOf(1, 2, 3, 4).forEach { total += it }
println(total) // 10Returning a Lambda from a Function
fun multiplierOf(factor: Int): (Int) -> Int {
return { n -> n * factor }
}
val double = multiplierOf(2)
val triple = multiplierOf(3)
println(double(5)) // 10
println(triple(5)) // 15Quick Check
Recap
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Passing Lambdas to Functions” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Passing Lambdas to Functions”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.
What will I learn in “Passing Lambdas to Functions”?
Pass lambdas as arguments and use trailing lambda syntax. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Kotlin Academy?
No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Passing Lambdas to Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Kotlin Academy lesson?
Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- What Is a Lambda? Syntax and Types
- Passing Lambdas to Functions
- it: The Implicit Parameter
- filter, map, and find on Collections