Declaring Functions
Explore the essential components of declaring functions in Kotlin. Understand the use of the fun keyword, parameter syntax, return types, and Kotlin-specific features like vararg and top-level functions. This lesson helps you grasp Kotlin’s streamlined function declaration style for clearer, more concise code.
Introduction
This is what the declaration of listOf in Kotlin really looks like:
public fun <T> listOf(vararg elements: T) : List<T>
Let’s go through this step by step:
The public keyword is no surprise for Java professionals.
However, the second keyword, fun, lets even experienced Java users down because there is no fun in Java (well, at least not as much as in Kotlin!).
Kotlin’s fun keyword
Just like variable declarations initiated with their own keywords (var or val), functions are also declared with a specific keyword. Its name, fun, stands for function.
The return type is last
As we have seen in variable declarations, their type is noted at the very end, after their name. This also applies to functions. Their return type follows the function name, and any parameters, separated by a :. Therefore, in our listOf() example, it is List<T>.
The order of the code syntax
Let’s look at how the different components of a function declaration and their order:
- Following the keyword fun, there is a generic type declaration
<T>— just like it is done in Java. - The fact that the name of the function,
listOf, is next, is no deviation from the Java syntax either. - If we omit
varargfor a moment and look at the declaration of the (single) parameter,elements, we notice the striking resemblance to a common variable declaration. First there is the name, then a:, which is followed by the type. Onlyvalorvaris missing. This is simply because, in Kotlin, function parameters are always implicitlyval, hencefinal. This is one of the numerous opportunities that Kotlin uses to implement best practices. These best practices came up over the years and Java has never been able to enforce them due to backward compatibility. - The
varargkeyword is simply Kotlin’s substitute for the...known from Java, for any number of arguments, which we would note down between the name and the type of the parameter.
As mentioned before, all that follows is the return type of the function, separated by a :, and with that, the function is fully declared.
Top-level functions
As we’ve established, functions may be declared without an outer class in Kotlin. Therefore, we can do without popular utility classes like CollectionUtils, which are nothing more than containers for static methods.
The
listOfis a similar top-level function provided by the Kotlin standard library in thekotlin.collectionspackage. Therefore, its fully qualified name iskotlin.collections.listOf. However, thanks to static imports, which we already know from Java, we rarely need a fully qualified name. Therefore, justlistOfis enough. We can also declare functions on the package level ourselves and benefit from this feature.
Named parameters, default parameters, and more
Functions in Kotlin offer numerous useful features that we might know from other languages. For example, when calling a function, we can reference parameters by name or save a lot of method overloading by using default values for arguments.
However, since we’ve just started to get familiar with Kotlin, and further details would throw us off track at this point, we’d like to defer this to later. Let’s look at the next line of the example (as seen in the previous lesson):
var lengths = strings.map {it.length}