Kotlin, Zero to Kotlin Hero

Zero to Kotlin Hero: Kotlin Functions

The previous article was about Null safety in Kotlin. In this article, get ready to understand Kotlin functions.

A function is a segment of a program that performs a specific task. Kotlin functions are declared with the fun keyword. The code snippet below is a function declared in Kotlin:

fun greetPerson(person: String) : String{
    return "Hello $person , nice to meet you!"


The basic syntax for declaring a Kotlin function is:

fun <name>(<argument1>:<type1>, <argument2>:<type2>..., <argumentn>:<typen>): <return type>

Kotlin also allows default arguments. This is used when a corresponding argument is omitted and they are used like this:

fun greetPerson(person: String = "Adora") : String{ ... }

In the snippet above, Adora is the default value of person, so Adora is used when there is no value for person.

Single Expression functions

Similar to Javascript Es6, if a function returns a simple expression, it is not important to add the curly brace and the function body can be treated as an expression. i.e: specified on the right after the = symbol.

An example is shown below. Instead of having a function

fun square(num: Int) : Int{ 
    return num * num

we write a shorthand of the same function. This shorthand would look like this:

fun square(num: Int) = num * num

Higher-order functions

Higher-order functions take a function as a parameter or return a function.


fun employ(unemployed: () -> Boolean): String{
    if (unemployed()){
       return "You have been employed"
    return "You already have a job "


fun isUnemployed (name : String): Boolean{
    return name.toLowerCase() === "betty"


In the example above, the employ function is the higher-order function because it takes the isUnemployed function as a parameter.

What happens here is that when the employ function is called, a condition is checked to see if the person is Betty (Betty is the only unemployed person). If it is, Betty gets the job. If its someone else, we know that they have already been employed.

This snippet illustrates how higher-order functions work. However, they are always used in more sophisticated ways.

This code below shows how to call the employ function:

fun main(){ 
   val employed = employ{isUnemployed("betty")}
   print (employed) 


Paste the snippets in your Kotlin editor and you should get this:

Inline functions

Higher-order functions have a disadvantage. Each function is an object and calls would cause a runtime overhead.

However, there is a resolution. The overhead can be eliminated by inlining the lambda expressions. An inline function is created by adding the inline keyword.

The inline modifier tells the compiler to copy the functions and parameters to call site, and this reduces the call overhead.

The sample function below is an inline function:

inline fun employ(unemployed: () -> Boolean): String{ ... }

Function Scope

In Kotlin, functions can be declared top-level (i.e outside a Kotlin class) unlike programming languages like Java or C#. They can also be either local or member functions.

Local functions:

A local function is a function declared inside a function. Let’s use preorder tree traversals for this example:

fun preorder(tree: Tree) { //outer
     val list = HashSet<Node>()
     fun preorder(current: Node){ //local
         if (current != null) {


The local function has access to the local variables of the outer function so the local preorder function has access to the list variable on the outer function. This is the explanation of the code snippet above:

Imagine we have a binary tree with multiple nodes and we are to do a preorder traversal on it. In the code snippet, the traversal starts from the root node to the left nodes and then the right nodes.

Here, the local function is being called recursively until the program terminates (until we reach a null node).

Member functions:

A member function is a function declared in a class. This is the most common way to declare functions in programming languages.

class AClass(){
    fun aFunction(){ ... }  //member function


We have seen the types of functions in Kotlin. Get ready to know about Kotlin data classes and sealed classes in the next article!



Spread the love

2 thoughts on “Zero to Kotlin Hero: Kotlin Functions

Leave a Reply

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