Top-level functions

In Kotlin, a function can be defined independently of any associated class. If a function is written within a Kotlin file, it will be available as a standalone, callable function within whatever visibility scope has been defined (public, internal, or private). This type of function is what's known as a top-level function.

This differs from other JVM languages such as Java and Scala where all functions are defined as methods of an associated class. This has implications within the Java interop story and will be discussed in more detail in Chapter 4, First-Class Functions. The following snippet demonstrates a basic, top-level function available within its declared module:

// defined within any *.kt class
fun printHello() = println("Hello!")