liveBook · Manning

Function type

As explained in Kotlin in Action, 2E

A function type describes the parameter types and return type of callable values.

A function type describes a callable value by its parameter types and return type. In Kotlin, it lets a variable, parameter, or return value hold a lambda or function reference, making behavior a value that can be passed, stored, and selected like other values. section 10.1.1 section 10.4

Declare and call a function value

```kotlin val sum: (Int, Int) -> Int = { x, y -> x + y } val action: () -> Unit = { println(42) } fun twoAndThree(operation: (Int, Int) -> Int) { println(operation(2, 3)) } twoAndThree { a, b -> a * b } // 6 ``` `(Int, Int) -> Int` means two `Int` inputs and an `Int` result. `() -> Unit` takes no arguments and has no meaningful result. Once a parameter has a function type, Kotlin can infer the lambda parameter types; call the passed function with ordinary call syntax, such as `operation(2, 3)`. section 10.1.1 section 10.1.2

Nullability placement matters. `(Int, Int) -> Int?` describes a function that may return `null`; `((Int, Int) -> Int)?` describes a value that may itself be `null`. Function-type declarations require an explicit return type, including `Unit`. Parameter names may be included for readability and IDE completion, but they do not affect type matching. section 10.1.1 section 10.1.2

Listing 10.1 Defining a simple higher-order function
fun twoAndThree(operation: (Int, Int) -> Int) {   #1
    val result = operation(2, 3)                  #2
    println("The result is $result")
}
 
fun main() {
    twoAndThree { a, b -> a + b }
    // The result is 5
    twoAndThree { a, b -> a * b }
    // The result is 6
}

Use a function-typed parameter to make one operation reusable with multiple lambda implementations. section 10.1.2

Function types underpin higher-order functions: functions can accept behavior as a parameter or return behavior chosen for a condition. A shipping calculator can return `(Order) -> Double`, while a contact filter can return `(Person) -> Boolean` for later use with `filter`. section 10.1 section 10.1.5

This is especially useful when repeated code differs in behavior rather than only data. Instead of hardcoding several site-visit queries, accept a `(SiteVisit) -> Boolean` predicate; callers can supply conditions for mobile visits or iOS signup visits. The same approach can replace interface-heavy variants of the strategy pattern with lambdas. section 10.1.6

Kotlin’s standard `filter`, `map`, and `sortedBy` APIs demonstrate the style: function types work with lambdas and member references to express collection-processing pipelines concisely. They support functional programming alongside ordinary mutable, object-oriented code. section 1.2.3

How Kotlin in Action, 2E covers it

How deep it goes
Explains syntax, invocation, nullability placement, named parameters, returned functions, and predicate-based reuse; defers lambda performance to inline functions.
Examples
Uses arithmetic operations, a String filter, shipping-cost selection, contact filtering, and site-visit duration queries.
What it assumes
Builds from lambda use and Kotlin type inference while relying on familiarity with Unit, nullable types, collections, and iteration.
What you can do after
Build it from scratch by declaring function-typed variables, parameters, and returns, then passing or invoking lambdas.
How it connects
Connects higher-order functions with lambda expressions, function references, nullable types, predicates, the strategy pattern, and inline functions.

Also explained in

Compare how 4 books explain function type →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Kotlin in Action, 2E. The section links point to the parts of the book it draws on.