Function Types & Literals
Higher order functions, function types, function literals, anonymous functions, lambdas, closures, lexical scope, function references and comparing anonymous functions vs. lambdas
— — — — — — — — — — — — — — —
THE CURRENT VERSION OF THIS ARTICLE IS PUBLISHED HERE.
— — — — — — — — — — — — — — —

This article is part of the Kotlin Primer, an opinionated guide to the Kotlin language, which is indented to help facilitate Kotlin adoption inside Java-centric organizations. It was originally written as an organizational learning resource for Etnetera a.s. and I would like to express my sincere gratitude for their support.
It is recommended to read the Introduction before moving on. Check out the Table of Contents for all articles.
In Kotlin, functions are first-class, which means they can be treated the same way as any other value type.
This means:
- They can be assigned to variables
- They can be passed as arguments to, and returned from, other functions. Functions accepting or returning other functions are called higher-order functions
- They have a type
Function types are denoted using e.g.(Int, String) -> String. If a function does not return a value, which is the same as saying it returns Unit, the type is e.g. (Int, String) -> Unit.
If a function does not accept parameters, it’s type is e.g. () -> String.
The function type can be instantiated using two different literals:
- the anonymous function
- the lambda function
Kotlin has closures, same as Java — a function can access the lexical scope it was defined it. It’s easier to understand when demonstrated:







