Function types
Kotlin supports functional programming, and functions are first-class citizens in Kotlin. A first-class citizen in a given programming language is a term that describes an entity that supports all the operations generally available to other entities. These operations typically include being passed as an argument, returned from a function, and assigned to a variable. The sentence a function is a first-class citizen in Kotlin should then be understood as: it is possible in Kotlin to pass functions as an argument, return them from functions, and assign them to variables. While Kotlin is a statically typed language, there needs to be a function type defined to allow these operations. In Kotlin, the notation used to define a function type is the following:
(types of parameters)->return type
Here are some examples:
(Int)->Int
: A function that takesInt
as an argument and returnsInt
()->Int
: A function that takes no arguments and returnsInt
(Int)->Unit
: A function that...