5
Anonymous Functions and the Function Type

In the last chapter, you saw how to define functions in Kotlin by naming them and how to call them by name. In this chapter, you will see another way to define functions: anonymously. You will be taking a short break from NyetHack to work with anonymous functions in your Sandbox project, but do not worry – there is more NyetHack action in the next chapter.

Functions like the ones you saw and wrote in Chapter 4 are called named functions. Functions defined without a name, called anonymous functions, are similar, with two major differences: Anonymous functions have no name as part of their definition, and they interact with the rest of your code a little differently in that they are commonly passed to or returned from other functions. These interactions are made possible by the function type and function arguments, which you will also learn about in this chapter.