Writing a basic function

Let's jump in and look at a basic function written in Kotlin:

// Main.kt
fun
helloFunctions(greeting: String) {
println("$greeting Kotlin Functions")
}

This snippet demonstrates a fully functional, and quite simple, function written in Kotlin. There are several interesting things to note in this example:

A function is written by starting with the fun keyword. This tells the compiler that we are writing a new function. The next component is the function name. In the preceding example, the name is helloFunctions. Functions in Kotlin follow the same naming conventions as methods in Java. Next, we see that we are defining a greeting parameter of the String type. This syntax is different than Java method parameters. In Kotlin, parameters are defined by providing the name first, then a colon, then the type.

It's also important to note that the previous snippet does not require an enclosing class. Unlike in Java, functions may exist on their own as top-level functions and can be used as standalone executable functions.