skip to content

Functional Features

Kotlin's functions-as-values layer: lambdas and their function types, method references, higher-order functions, and the inline machinery that keeps them cheap. The performance half of this area is what distinguishes a strong answer.

part ofKotlinoverview, primer and where to startread it →
on this pageshow

explore

questions

110 · 6 sections

In Kotlin, what does a function type like (Int) -> String mean, and how can you store a function in a variable?

level: juniorimportance: must knowfreq 80%
basics
~20 s

A function type describes a function's inputs and output. (Int) -> String means: takes an Int, returns a String. You can put such a function in a variable and call it later like a normal function.

open as a page

What is a higher-order function in Kotlin? Show how to write one that takes a function as a parameter and one that returns a function.

level: middleimportance: must knowfreq 70%
basics
~10 s

A higher-order function takes a function as an argument, returns a function, or both. For example, map takes a transform function. You declare the parameter with a function type and call it inside.

open as a page

What is the difference between a lambda, an anonymous function, and a callable (function/property) reference in Kotlin, and when would you use each?

level: middleimportance: must knowfreq 75%
basics
~20 s

All three produce a function value. A lambda is the short { } form. An anonymous function uses fun(...) {...} with an explicit return type. A reference like ::foo points at existing code instead of rewriting it.

open as a page

What object does a Kotlin lambda actually become at runtime, and what are the performance implications of passing functions as values?

level: seniorimportance: should knowfreq 55%
basics
~10 s

A lambda becomes an object implementing a FunctionN interface (like Function1) with an invoke method. Creating these objects and capturing variables can add allocations, which inline functions can remove.

open as a page

How do Kotlin function types relate to Java functional (SAM) interfaces, and when does SAM conversion apply?

level: seniorimportance: should knowfreq 45%
basics
~10 s

Java interfaces with one method (like Runnable) can accept a lambda through SAM conversion. Kotlin's own function types are different objects, but you can mark a Kotlin interface 'fun interface' to allow lambdas too.

open as a page

What does it mean that a Kotlin lambda 'closes over' a variable, and what can a lambda do with a captured local variable that a Java lambda cannot?

level: juniorimportance: must knowfreq 70%
basics
~10 s

A Kotlin lambda can read and remember variables from the function around it. Unlike Java, it can also change a captured var, not just read it.

open as a page

What is the basic brace syntax of a Kotlin lambda, and what does the implicit `it` refer to?

level: juniorimportance: must knowfreq 85%
basics
~20 s

A lambda is code in curly braces: { x -> body }. When it takes exactly one parameter you can skip naming it and use the built-in name it instead, like list.map { it * 2 }.

open as a page

In a multi-line Kotlin lambda, how is the value the lambda produces determined? Show how this works with a map call.

level: juniorimportance: must knowfreq 78%
basics
~10 s

The last line in the lambda's body is its result automatically. You don't write a return keyword. Whatever that final line evaluates to is what the lambda hands back.

open as a page

What is the trailing-lambda convention in Kotlin, and how do you write `list.filter { it > 0 }` using it?

level: juniorimportance: must knowfreq 80%
basics
~20 s

If the last argument to a function is a lambda, you can write it outside the parentheses. So instead of filter({ it > 0 }) you write filter { it > 0 }, which reads more cleanly.

open as a page

How does a bare 'return' behave inside an anonymous function versus inside a lambda passed to forEach? Explain local vs non-local return.

level: middleimportance: must knowfreq 60%
basics
~20 s

In an anonymous function, 'return' exits just that function. In a lambda, a bare 'return' exits the whole enclosing function (a non-local return). That different behavior is the main reason to pick an anonymous function.

open as a page

You have a property `val onClick: (() -> Unit)?`. How do you call it safely when it might be null, and what is the difference between `onClick?.invoke()` and `onClick()`?

level: juniorimportance: must knowfreq 70%
basics
~10 s

Use onClick?.invoke(). It runs the function only if it is not null, otherwise it does nothing. Calling onClick() directly would not compile because the type may be null.

open as a page

How do you read and write a plain Kotlin function type such as (Int, Int) -> String, and what does () -> Unit mean?

level: juniorimportance: must knowfreq 80%
basics
~20 s

It describes a value that is itself a function. The part in parentheses lists the input types, the arrow points to the result type. () -> Unit means: takes no input and returns nothing useful.

open as a page

What is a receiver function type like String.() -> Unit in Kotlin, and how does it differ from a plain function type (String) -> Unit?

level: juniorimportance: must knowfreq 70%
basics
~20 s

A receiver function type is a lambda that runs 'inside' an object, so you can use this and call its members directly without naming it. A plain function type just takes the object as a normal parameter you must name.

open as a page

What is a function-type typealias in Kotlin, and why would you declare something like `typealias Handler = (Event) -> Unit`?

level: juniorimportance: must knowfreq 55%
basics
~10 s

It gives a short, meaningful name to a function type. Instead of writing (Event) -> Unit everywhere, you write Handler. It is just an alias, so the two names mean exactly the same type.

open as a page

What is a suspend function type such as `suspend () -> T`, and where can a value of that type be invoked?

level: middleimportance: must knowfreq 60%
basics
~20 s

It is the type of a function that can suspend, like a lambda you can pause and resume. You can only call it from inside a coroutine or another suspend function, not from ordinary code.

open as a page

What is the difference between a bound reference like `instance::method` and an unbound reference like `Type::method` in Kotlin?

level: juniorimportance: must knowfreq 55%
basics
~10 s

A bound reference already remembers which object to call the method on, so you just pass arguments. An unbound reference does not; you must pass the object itself as the first argument.

open as a page

What does the `::` operator do when written as `list.map(::println)`, and how is it different from writing `list.map { println(it) }`?

level: juniorimportance: must knowfreq 70%
basics
~20 s

::println turns an existing function into a value you can pass around. It does the same job as the lambda { println(it) }, just shorter, because you reuse a function that already exists instead of writing a new one.

open as a page

What is a property reference like ::name or Person::name in Kotlin, and what can you do with the value you get?

level: juniorimportance: must knowfreq 55%
basics
~10 s

It is a value that points to a property instead of reading it. You can pass it to functions like map to read that property from many objects, for example people.map(Person::name).

open as a page

Given `class Box(val n: Int) { fun scaled(f: Int) = n * f }`, what are the function types of `Box::scaled` and `box::scaled`, and how do you invoke each?

level: middleimportance: must knowfreq 45%
basics
~10 s

Box::scaled needs both the box and the factor, so its type is (Box, Int) -> Int. box::scaled already has the box, so it only needs the factor: (Int) -> Int.

open as a page

How does a constructor reference (::ClassName) work, and how would you use it as a factory passed to map?

level: middleimportance: must knowfreq 50%
basics
~20 s

::ClassName is a value that creates new objects of that class. Its parameters match the constructor, so you can pass it where a function is expected, like ids.map(::User) to turn each id into a User.

open as a page

What does the crossinline modifier do on a lambda parameter of an inline function?

level: juniorimportance: must knowfreq 55%
basics
~20 s

crossinline marks a lambda of an inline function so it cannot use a non-local return. It is still inlined, but you are not allowed to write 'return' to exit the calling function from inside it.

open as a page

What does the `inline` modifier do to a Kotlin function, and why is it most useful on functions that take lambda parameters?

level: juniorimportance: must knowfreq 80%
basics
~20 s

inline tells the compiler to copy the function's body straight into each place it is called, instead of making a real function call. This avoids creating an object for the lambda you pass in, so it runs faster.

open as a page

Why does Kotlin offer the `inline` keyword for higher-order functions, and what runtime cost does inlining remove?

level: juniorimportance: must knowfreq 70%
basics
~20 s

Normally passing a lambda creates an extra object and an extra method call. Marking the function inline copies the lambda's code straight into the caller, so no object is created and no extra call happens.

open as a page

What does the `noinline` modifier do when applied to a lambda parameter of an `inline` function?

level: juniorimportance: must knowfreq 55%
basics
~20 s

It tells Kotlin NOT to inline that one lambda. Instead the lambda stays a normal object you can store in a variable or pass to another function, while the other lambdas in the inline function are still inlined.

open as a page

What is a non-local return in Kotlin, and why does `return` inside `forEach { }` exit the enclosing function?

level: juniorimportance: must knowfreq 60%
basics
~20 s

A plain return inside a lambda can jump out of the whole surrounding function, not just the lambda. This works in forEach because forEach is an inline function, so the lambda's body is copied directly into your code.

open as a page

What does it mean to compose two functions in Kotlin, and how would you write an `andThen` helper that runs one function and feeds its result into another?

level: juniorimportance: must knowfreq 55%
basics
~10 s

Composing means chaining functions so one's output becomes the next's input. An andThen helper takes function f, then g, and returns a new function that does g(f(x)) for any input x.

open as a page

What is a higher-order function in Kotlin, and how do you declare a function that takes another function as a parameter?

level: juniorimportance: must knowfreq 85%
basics
~10 s

A higher-order function takes a function as a parameter or returns one. You declare the parameter with a function type like (Int) -> Int, then call it inside the body.

open as a page

Implement both `compose` and `andThen` as infix extensions on function types and explain why the generic signatures differ in argument order.

level: middleimportance: must knowfreq 45%
basics
~10 s

andThen runs the receiver first; compose runs the argument first. Their type parameters differ because the receiver and argument swap which function produces the intermediate value the other consumes.

open as a page

Why does idiomatic Kotlin usually prefer default and named arguments over currying or hand-rolled partial application? Give a concrete comparison.

level: middleimportance: must knowfreq 45%
basics
~20 s

Kotlin lets you give parameters default values and pass arguments by name, so you can omit or fix arguments at the call site without building chains of lambdas. It is clearer and avoids extra objects.

open as a page

What is currying in Kotlin, and how do you encode a two-argument function like add(a, b) as a curried function using returned lambdas?

level: juniorimportance: should knowfreq 35%
basics
~20 s

Currying turns a function that takes many arguments into a chain of functions that each take one argument and return the next function. You do it in Kotlin by returning a lambda from a lambda.

open as a page