Lambda Return Values in Kotlin
On this page (12sections)
Introduction
Lambda Return Values is a fundamental concept every Kotlin developer should understand. Lambda expressions are anonymous functions you can pass as values — the foundation of functional-style APIs in Kotlin.
The last expression in a lambda body is returned automatically. In this tutorial you will learn the syntax, walk through a complete example program, study the sample output, and review best practices so you can apply the concept confidently in your own projects.
Definition
- The last expression in a lambda body is returned automatically.
- Use return@label for explicit return from lambda.
- Lambdas can return any type.
Syntax
list.filter { it > 0 }
Lambda Return Values in Kotlin Example Program in Kotlin
fun main(args: Array<String>) {
val numbers = listOf(1, -2, 3, -4)
val positives = numbers.filter { it > 0 }
println(positives)
}
Sample Output
[1, 3]
When to use
Use lambdas for short callbacks passed to collection operations, event handlers, or higher-order functions.
How it works
-
The program starts with a
mainfunction — the entry point that runs when you execute the file. -
val numbers = listOf(1, -2, 3, -4)assigns or updates a value used later in the program. -
val positives = numbers.filter { it > 0 }assigns or updates a value used later in the program. -
The
println(positives)statement writes a line to the console — this produces part of the sample output below. -
The last expression in a lambda body is returned automatically.
-
Run the program in IntelliJ IDEA, Android Studio, or with the Kotlin command-line compiler (
kotlinc/kotlin). Compare your console output with the sample output shown below.
Best Practices
- Keep lambdas short; extract longer logic into named functions.
- Use
itonly when the lambda has a single parameter and the meaning is obvious. - Prefer trailing lambda syntax when the last argument is a function.
Common Mistakes
- Capturing mutable variables unsafely in concurrent lambdas.
- Over-nesting lambdas when a named function would be clearer.
- Forgetting that return inside a lambda returns from the lambda, not the outer function (unless inline).
Key Points
- The last expression in a lambda body is returned automatically.
- Use return@label for explicit return from lambda.
- Lambdas can return any type.
- Test the example locally and verify the output matches the sample.
- Experiment by changing input values to see how behaviour changes.
Notes
- Semicolons at the end of statements are optional in Kotlin.
Frequently Asked Questions
What is Lambda Return Values in Kotlin?
When should I use Lambda Return Values?
How is Lambda Return Values different from Java?
How do I practice this topic?
Related Tutorials
Lambda with Parameters in Kotlin
Learn Lambda with Parameters in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorialAnonymous Functions in Kotlin
Learn Anonymous Functions in Kotlin with clear explanation, syntax, example program, sample output, best practices, and FAQs.
Read tutorial