0Pricing
Android Academy · Lesson

Lambdas and Higher-Order Functions

Pass behavior as values.

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

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.

Frequently asked questions

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

Yes — the full text of “Lambdas and Higher-Order Functions” is free to read here on the web, and the Android 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 Android Academy course, upgrade to CoddyKit PRO.

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

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