Lambdas & Higher-Order Functions

Kotlin is a modern, concise language that treats functions as values — you can write anonymous lambdas and pass them into higher-order functions, which is the heart of its expressive style.

Learn Lambdas & Higher-Order Functions in our free Kotlin course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a…

Part of the free Kotlin course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll write lambdas, use the implicit it parameter, and create functions that accept other functions.

What You'll Learn in This Lesson

1️⃣ Lambdas

A lambda is an anonymous function written as {' '} . You can store it in a variable, whose type is a function type like (Int) - Int . When a lambda has a single parameter you can refer to it as it . The last expression is the return value.

Storing behaviour in a value like this is the gateway to functional programming — you can pass these around just like numbers or strings.

2️⃣ Higher-Order Functions

A higher-order function accepts a function as a parameter (or returns one). The parameter's type is a function type. Because the function is the last parameter, you can use trailing-lambda syntax and place the lambda outside the parentheses.

The same compute function did addition, subtraction, and multiplication just by changing the lambda. That's the power of abstracting over behaviour.

Your turn. Replace the TODO , then run and compare.

Write a higher-order function that delegates formatting to a lambda, then call it with two different formatters.

📋 Quick Reference — Lambdas

Practice quiz

What is a lambda in Kotlin?

  • A named top-level function
  • An anonymous function written as { params -> body }
  • A class with one method
  • A reserved keyword

Answer: An anonymous function written as { params -> body }. A lambda is an anonymous function literal written as { params -> body }.

What is the type of the lambda { n: Int -> n * n }?

  • Int
  • (Int) -> Int
  • () -> Int
  • (Int, Int) -> Int

Answer: (Int) -> Int. It takes one Int and returns an Int, so its type is (Int) -> Int.

What is the return value of a lambda?

  • Always Unit
  • The first expression
  • Whatever follows the return keyword
  • Its last expression

Answer: Its last expression. A lambda returns the value of its last expression.

In a single-parameter lambda, what implicit name refers to the argument?

  • it
  • arg
  • this
  • value

Answer: it. A single-parameter lambda can use the implicit name it.

What is a higher-order function?

  • A function with many parameters
  • A function defined at file top level
  • A function that takes or returns another function
  • A recursive function

Answer: A function that takes or returns another function. A higher-order function takes a function as a parameter, returns one, or both.

When can you use trailing-lambda syntax?

  • When the function's last parameter is a function type
  • Only with no other arguments
  • When the lambda is first
  • Never with collections

Answer: When the function's last parameter is a function type. If the last parameter is a function type, the lambda can go outside the parentheses.

What does compute(6, 2) { x, y -> x + y } evaluate to in the lesson?

  • 12
  • 4
  • 8
  • 62

Answer: 8. The lambda adds: 6 + 2 = 8.

Why must a two-parameter lambda name its parameters?

  • it only works for a single parameter
  • Lambdas can't take two parameters
  • Naming is optional but recommended
  • To avoid recursion

Answer: it only works for a single parameter. it exists only for one parameter; with two you must name them, like { a, b -> ... }.

What does a bare 'return' inside a lambda try to do?

  • Return the lambda's last value
  • Exit the enclosing function
  • Restart the lambda
  • Return null

Answer: Exit the enclosing function. A bare return targets the enclosing function; a lambda's value is its last expression.

Which method reference replaces { it.uppercase() } in list.map?

  • uppercase::String
  • ::uppercase
  • String::uppercase
  • it::uppercase

Answer: String::uppercase. String::uppercase is a method reference equivalent to { it.uppercase() }.