0Pricing
Kotlin Academy · Lesson

What Is a Lambda? Syntax and Types

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

What Is a Lambda? Syntax and Types is a free Kotlin Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “What Is a Lambda? Syntax and Types” lesson free?

Yes — the full text of “What Is a Lambda? Syntax and Types” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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