Declaring Functions in Kotlin With fun

Declaring functions in Kotlin is simple. We have a few different ways to do it. For example we can skip defining the return type if the function returns an expression and does not have a function body. Or, we can set function parameters to have default values. And many more cool features! But we’ll take a look at them now one by one.

Declaring Functions With Function Body

Every function in Kotlin has something called a function body or an expression body. We say that a function has a function body if its’ logic lives inside the curly braces ({}).

fun sum(a: Int, b: Int): Int {
    return a + b
}

Declaring functions in Kotlin starts with a keyword fun. This is followed by the function name sum. Then, in the parenthesis we have the parameters (a, b). And the return type Int comes after the parameters being separated by a colon.

Functions With Expression Body

We can simplify the above sum function a bit more. As the function body contains only a single expression we can omit the return type and also the return keyword. Now, we can say that our sum function has an expression body instead:

fun sum(a: Int, b: Int) = a + b

Suddenly our function became much more compact and easier to read thanks to less clutter in the code. Kotlin has a feature called type inference that lets us skip defining the return type of the function. This is simply because the compiler is able to analyze the code and figure out the return type from the context of the code. To point out, we can only omit the return type if the function has an expression body.

Declaring Functions in Kotlin With Default Parameter Values

Having the possibility to declare functions in Kotlin with default parameter values is a great feature. Personally I use it quite often. One of the perks of default arguments is that it helps us avoid having overloaded functions (like we usually do in Java for example).

Next, let’s take a look at a simple example of how we can use default parameter values.

fun sayHello(name: String = "guest") = println("Hello $name!")

fun main(args: Array<String>) {
    sayHello("Ada") // Output: Hello Ada!
}

In the example above we have declared that the parameter name has a default value guest. The default value is declared after the parameter name and =. There’s no requirement to use the default value if we don’t want to. So we can still send the parameter into the function when we call it and it will be used in the expression body. In our case, we sent in the name to be Ada.

Next follows an example of how to omit the parameter and use the default value instead:

fun sayHello(name: String= "guest") = println("Hello $name!")

fun main(args: Array<String>) {
    sayHello() // Output: Hello guest!
}

Conclusion

Declaring functions in Kotlin can be done by using the fun keyword. A function can have either a function body or an expression body. In case we have a function with an expression body we can omit the return type and the return keyword to make the code more concise. Also, when needed, we can declare our functions in Kotlin to have default parameter values.

Be the first to reply

Leave a Reply

Your email address will not be published. Required fields are marked *