Kotlin Tour



Kotlin makes programming fun again so naturally the fun keyword is used to declare a function 😀.

Declare a function add which adds two Ints
fun add(a: Int, b: Int): Int = TODO()

If we only have one return statement we can transform the function to use only an expression.

Rewrite the previous code using a single expression
fun add(a: Int, b: Int): Int = TODO()

We could go one step further and remove the return type as the compiler will be nice enough to infer it for us.

Drop as much as you can
fun add(a: Int, b: Int): Int = TODO()

Note: inferring the return type works only for single expression function, the compiler won't try to infer it for functions with block body so overuse single expression function 😁.

If a function doesn't return anything we use the Unit type :

Declare a function printing a meaningful sentence
fun printTheAnswer(): Unit = TODO()

Named arguments

When calling a function we can change arguments order by naming them in the call :

fun sub(a: Int, b: Int) = a - b

print(sub(b = 1, a = 2))        // 1

Default arguments

Never dreamt of assigning default values to some arguments instead of having to define multiple overloading functions ? Guess what ? You can 😀 :

Declare a function strEq returning either two strings are the same or not, ignoring or not their case
fun strEq(s1: String, s2: String): Boolean = TODO()
fun strEq(s1: String, s2: String, ignoreCase: Boolean): Boolean = TODO()

Tip : use @JvmOverloads to generate overloading methods for Java code.

Local Functions

You can declare local functions, having access to outer scope :

Define a function checking if a number is even or not
fun isEven(n: Int): Boolean {
fun isMultiple(operand: Int): Boolean = TODO()


It is possible to extend existing classes with new functionalities, even existing JVM classes.
We just need to prefix the function name with the receiver type, followed by .. The receiver will be accessible as this in the function.

Create a function tail returning the tail of a given string
fun tail() = TODO()

Infix notation

Using infix keyword and extension functions we can use infix notation for some of our functions :

Declare an infix function inc that can increment an Int
fun inc() = TODO()


As in Java we might need to pass a variable number of arguments. In this case we need to prefix the argument by the keyword vararg :

Declare a function sum that can sum an infinite number of Ints
fun sum(vararg xs: Int): Int = TODO()

Operator Overloading

At last we can overload common operators using the operator keyword 😃 :

Define a plus operator to add two Points a function sum that can add an infinite number of Ints
import java.awt.Point
operator fun Point): Point = TODO()

Tail recursion

A tail recursive function can be marked as such with the tailrec keyword.

Define a freq tail recursive function counting the number of a character in a string
fun freq(s: String, c: Char): Int = TODO()

Beware: contrary to Scala the compiler won't raise an error if you incorrectly marked a function as tail recursive.

Create your playground on
This playground was created on, our hands-on, knowledge-sharing platform for developers.
Go to
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
Online Participants