Kotlin Academy · Lesson

What Is a Lambda? Syntax and Types

Understand lambda expressions: syntax, parameter types, and return values.

Lesson 1 of 413 steps

What Is a Lambda? Syntax and Types is a free Kotlin Academy lesson on CoddyKit. This is lesson 1 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.

Lambdas in One Sentence

A lambda is an anonymous function — a function value you can store in a variable, pass to another function, or return.

Basic Lambda Syntax

A lambda is written between curly braces: parameters on the left of ->, body on the right.

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

Lambda with No Parameters

Omit parameters entirely if there are none. The last expression is the return value.

fun main() {
    val random = { (1..6).random() }
    println(random()) // some number 1..6
}

Multi-Parameter Lambda

Separate parameters with commas. Specify types or rely on inference from context.

fun main() {
    val sum = { a: Int, b: Int -> a + b }
    println(sum(3, 4)) // 7
}

Inferring Lambda Types

When the lambda is assigned to a typed variable, parameter types can be omitted.

fun main() {
    val multiply: (Int, Int) -> Int = { a, b -> a * b }
    println(multiply(6, 7)) // 42
}

Lambda Type Annotations

The type (Int, Int) -> Int describes a function taking two Ints and returning an Int. This is how lambdas are typed.

fun main() {
    val op: (Int) -> Int = { x -> x * x }
    println(op(5)) // 25
}

Lambda Returning Unit

If the lambda has no useful return value, its type is () -> Unit. Unit is Kotlin's equivalent of void.

fun main() {
    val log: (String) -> Unit = { msg -> println("[LOG] $msg") }
    log("Hello")
    log("World")
}

Storing Lambdas in a List

Lambdas are values — you can collect them and call them later.

fun main() {
    val operations: List<(Int) -> Int> = listOf({ it + 1 }, { it * 2 }, { it - 3 })
    for (op in operations) println(op(10))
    // 11, 20, 7
}

Returning a Lambda

A function can return a lambda, capturing its environment in a closure.

fun makeAdder(n: Int): (Int) -> Int = { x -> x + n }
fun main() {
    val add5 = makeAdder(5)
    println(add5(10)) // 15
    println(add5(20)) // 25
}

Lambdas Capturing Variables

Lambdas can read and (for var) modify variables from their enclosing scope.

fun main() {
    var counter = 0
    val increment = { counter++ }
    increment(); increment(); increment()
    println(counter) // 3
}

Lambdas vs Anonymous Functions

Kotlin also has fun(x: Int): Int = x * 2-style anonymous functions. They behave like lambdas but support explicit return types and labeled return.

fun main() {
    val square = fun(x: Int): Int { return x * x }
    println(square(8)) // 64
}

Quick Check

What does the type (Int) -> String describe?

Recap

A lambda is an anonymous function written { params -> body }. Its type is (ParamTypes) -> ReturnType. Lambdas can be stored, passed, returned, and capture variables from their enclosing scope.

Free to start

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 “What Is a Lambda? Syntax and Types” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “What Is a Lambda? Syntax and Types”, 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 “What Is a Lambda? Syntax and Types”?

Understand lambda expressions: syntax, parameter types, and return values. 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 1 of 4.

How long does the “What Is a Lambda? Syntax and Types” 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

  1. What Is a Lambda? Syntax and Types
  2. Passing Lambdas to Functions
  3. it: The Implicit Parameter
  4. filter, map, and find on Collections
← Back to Kotlin Academy