Functions
Functions are one of the ways in which we can organize code in a more modular fashion. Modular code is easier to write, test, understand and maintain. Modularity also facilitates reuse.
A Kotlin function can be defined in two ways: with a block body or with an expression body.
In addition, we can create blocks of code known as lambda expressions, which are a bit like ‘anonymous functions’.
Functions can also be categorized according to the scope of the function definition. We can write standalone functions that exist on their own, separate from any other structure. We can also write extension functions and member functions that are associated with specific classes.
After completing the work in this section, you will be able to create functions with block and expression bodies, and you will be able to call those functions using both positional and named arguments. You will also be able to write extension functions that ‘plug in’ to an existing type, extending its capabilities.
You will learn about lambda expressions and member functions later.