Android Academy · Lesson

Lambdas and Higher-Order Functions

Pass behavior as values.

Lesson 4 of 413 steps

Lambdas and Higher-Order Functions is a free Android Academy lesson on CoddyKit. This is lesson 4 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 Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

What Is a Lambda?

A lambda is a small function you can write inline, without giving it a name.

You wrap it in curly braces: { x -> x + 1 }. Lambdas let you pass behavior around like data.

You already used them with map and filter.

Storing a Lambda in a Variable

A lambda is a value, so you can store it in a variable and call it later.

Call it like a function using parentheses.

fun main() {
    val square = { x: Int -> x * x }
    println(square(5))
    println(square(3))
}

The it Shortcut

When a lambda takes exactly one parameter, you can skip naming it and use it.

This keeps short lambdas compact and readable.

fun main() {
    val nums = listOf(1, 2, 3, 4)
    val big = nums.filter { it > 2 }
    println(big)
}

Multiple Parameters

A lambda can take more than one parameter, separated by commas before the arrow.

The last expression in the lambda is its return value.

fun main() {
    val add = { a: Int, b: Int -> a + b }
    println(add(4, 6))
}

Higher-Order Functions

A higher-order function is a function that takes another function as a parameter or returns one.

The function type (Int) -> Int means "takes an Int, returns an Int". This is how map and filter work.

Writing a Higher-Order Function

Here we define applyTwice, which accepts a function and runs it two times.

The parameter op has the type (Int) -> Int.

fun applyTwice(x: Int, op: (Int) -> Int): Int {
    return op(op(x))
}

fun main() {
    println(applyTwice(3) { it + 1 })
}

Trailing Lambda Syntax

When a lambda is the last argument, you can move it outside the parentheses.

That is why list.filter { ... } looks so clean. Both forms below are equal.

fun main() {
    val nums = listOf(1, 2, 3)
    println(nums.map({ it * 2 }))
    println(nums.map { it * 2 })
}

Passing Named Functions

You can pass an existing named function where a lambda is expected using ::, called a function reference.

This avoids rewriting logic as a lambda.

fun isOdd(n: Int) = n % 2 == 1

fun main() {
    val nums = listOf(1, 2, 3, 4)
    println(nums.filter(::isOdd))
}

Returning a Function

A higher-order function can also return a function. Here multiplier builds a custom multiplying lambda.

This is a simple form of configuration.

fun multiplier(factor: Int): (Int) -> Int {
    return { it * factor }
}

fun main() {
    val triple = multiplier(3)
    println(triple(5))
}

Lambdas Capture Variables

A lambda can read variables from the surrounding scope. This is called a closure.

Here the lambda remembers bonus even after it is passed elsewhere.

fun main() {
    val bonus = 10
    val nums = listOf(1, 2, 3)
    println(nums.map { it + bonus })
}

Why Lambdas Matter on Android

On Android, lambdas express callbacks cleanly. A button click listener is just a lambda you hand to the framework.

For example: button.setOnClickListener { showMenu() } runs your code when tapped.

Quick Check

Test your understanding of lambdas and higher-order functions.

Recap

Lambdas are unnamed inline functions written in braces, using it for a single parameter.

Higher-order functions take or return functions, enabling clean callbacks and reusable operations like map and filter.

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
36
Lessons
152

Frequently Asked Questions

Is the “Lambdas and Higher-Order Functions” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Lambdas and Higher-Order 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 Android Academy course includes 4 lessons in total.

What will I learn in “Lambdas and Higher-Order Functions”?

Pass behavior as values. You practise Android 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 Android Academy?

No prior experience is required. Android 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 4 of 4.

How long does the “Lambdas and Higher-Order 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 Android Academy lesson?

Yes. Every Android 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. Lists and Mutable Lists
  2. Maps and Sets
  3. map, filter, forEach
  4. Lambdas and Higher-Order Functions
← Back to Android Academy