In previous chapters, we've seen Kotlin variables, type systems, and control structures. But to create applications, we need building blocks that allow us to make structures. In Java, the class is the building block of the code. Kotlin, on the other hand, supports functional programming; therefore, it is possible to create whole programs or libraries without any classes. The function is the most basic building block in Kotlin. This chapter introduces functions in Kotlin, together with different function features and types.
In this chapter, we will cover the following topics:
- Basic function usage in Kotlin
- The Unit return type
- The vararg parameter
- Single-expression functions
- Tail-recursive functions
- Default argument values
- Named argument syntax
- Top-level functions
- Local functions
- The Nothing return type