The fun of Kotlin - Local Functions

The fun of Kotlin - Local Functions

In Kotlin, functions are treated as first-class citizens. This series is dedicated to exploring the extraordinary features of Kotlin functions.

Topics We'll Cover

  1. Local functions (you’re here)

  2. Infix functions

  3. Anonymous functions

  4. Inline functions

  5. Returns and local returns

Who Is This For?

This series is designed for all Kotlin enthusiasts, including those who are currently learning or have an interest in learning Kotlin.

Prerequisite

Before proceeding, I recommend you familiarize yourself with the basic syntax of the Kotlin language, such as how to define variables, classes, and functions. The official Kotlin website is an excellent place to start.

Local functions

A local function is a function declared within another function. Yes, Kotlin allows for such nested function definitions. Here's an example:

fun outerFunction(nice: String) {
    val hello = "Hello, world"

    fun innerFunction(awesome: String) {
        println(awesome)
        println(nice)    // Accessing argument of outer function
        println(hello)   // Accessing variable declared in outer function
    }

    innerFunction("This is awesome") // Invoking the inner function
}

fun main(args: Array<String>) {
    outerFunction("This is nice") // Only outerFunction is accessible here
}

Local functions serve a practical purpose and can help us to create cleaner and more maintainable code. They are typically used when a certain piece of logic is needed repeatedly only within a specific function, and nowhere else in the class or program. In other languages, we might be tempted to extract this logic into a private method. However, this can lead to bloated classes where private methods used by only one function are cluttering up the space and possibly obscuring the class's main purpose.

In contrast, Kotlin's local functions allow us to encapsulate the logic neatly within the function that uses it. This keeps our classes clean and focused, improves readability, and makes it easier to understand the code flow. Moreover, local functions have access to variables and parameters of the outer function, which means they don't need any extra arguments to operate on this data. This reduces the chances of error by limiting the scope of these variables and ensuring they are not accidentally modified elsewhere.

So, local functions help us to reduce redundancy, improve code readability, and ensure better encapsulation within our Kotlin functions. Note that access modifiers like public and private do not apply to local functions.

Further Reading

If you'd like to explore more about local functions and other Kotlin concepts, the following resources could be of great help:

  1. Kotlin Official Documentation: The official Kotlin documentation is the primary source for understanding Kotlin's syntax and features in depth. The section on Functions covers local functions and many other types of functions in Kotlin.

  2. Kotlin for Java Developers (Coursera): This course can help you understand Kotlin concepts if you're coming from a Java background.

  3. Kotlin in Action (Book): This book provides a comprehensive guide to the Kotlin language for experienced Java developers.

Remember, the more you practice, the more fluent you'll become in Kotlin. There are several online platforms like LeetCode, HackerRank, and Exercism where you can solve Kotlin-based coding problems to sharpen your skills.

In the next article, we'll explore another interesting feature of Kotlin functions - Infix functions. Stay tuned!


Thank you for taking the time to read this article. If you have any questions, or suggestions on how to improve this article, please feel free to contact me on Twitter :)

Jagdeep Singh