With Kotlin you can do much more with functions as compared to JAVA. Lets explore some of the features related to functions offered by Kotlin.

If you are completely new to functions in Kotlin, then please read this: Getting Started with Kotlin – Tutorial 4 – Functions

Local Functions

In Kotlin, you can define function inside of functions. These are called local functions. Here is an example:

The local functions has complete access to the variables defined in the enclosing functions. You can define as many local functions as you want.

Infix Functions

If your function is an extension function and takes only one argument then kotlin provides a more pragmatic way to call the function using the infix keyword.

Lets say I have an extension function on ‘Int’ that takes another ‘Int’ as an argument and tell whether it is greater or not.

The function call will look something like this.

What we have to do is just add the infix keyword before the function.

By doing this we can call the function more pragmatically.

A very small feature, but one that makes the code look a whole lot better. Nice!

Returns and Local Returns

Suppose you have a function inside which you have are calling another function that takes in a lambda function. Lets take an example, a function named outerFunction contains an IntRange from 1 to 100, and using the forEach function on the range object we are looping over the range. (forEach function takes in a lambda)

Now inside the forEach I want to exit the loop when the value reaches 25. You might think the solution might be putting a return keyword at the end of forEach loop,

Well, this is not the solution, “Hello” will not be printed because adding a return statement will not return the control from the forEach but it will return the control from the outerFunction itself. You might have faced the same issue in other programming languages as well. Kotlin has a fix for it, called labels. The basic idea is that you will explicitly tell kotlin from which scope to return the control flow. Lets see how we will return from forEach.

After the return keyword we specify the name from which we want to return. The name ‘forEach’ is available by default because we are inside of forEach loop. Now suppose there is a forEach inside a forEach, the complier will not know which one you are referring to. In this case we use custom labels, we will explicitly give a label name to the scope blocks.

In the highlighted part above you can see the syntax for providing a custom label, the name of label followed by @.

These are some of the many powerful features provided by Kotlin. I hope you have learned something new or have brushed up on some of the concepts that you already knew.

Gurleen Sethi

Android Developer and enthusiast, believes in developing products and content to help people. Likes to do deep thinking.