Nested Functions in Kotlin : Example Usage

Home » Kotlin » Nested Functions in Kotlin : Example Usage

Nested functions in Kotlin allow developers to organise their code more effectively by defining functions within other functions. This powerful feature enhances code readability and maintainability. In this Kotlin tutorial, we’ll dive into the concept of nested functions in Kotlin, exploring the syntax, benefits, and best practices.

nested functions in kotlin example and use case

Understanding Nested Functions in Kotlin

Nested functions refer to the creation of functions within other functions. In Kotlin, this means defining a function inside another function, This nested structure enables encapsulation, ass the inner function can only be accessed with the scope of the outer function.

Syntax of Nested Functions

To declare a nested function in Kotlin, simply define a function within the body of another function.

fun outerFunction() {

// write outer function code here

fun innerFunction() {

  // inner function code here
// code calling innerFunction()



fun main() {

fun outerFunction()
    println("I am from outer function")
    fun innerFunction(){
        println("I am an inner function ")
    println("I am here!")


I am from outer function
I am here!

Example Usage

Imagine a situation in which an outer function computes different metrics while an inner function aids in executing particular calculations.

Open the Kotlin Playground Write the following code:

fun main() {
    val number = listOf(5,5,5,100)
    println("Sum & Avg : " + calc(number))

fun calc(values: List<Int>): Pair<Double, Double>{
    var sum = 0
    fun calcSum(){
        for (number in values)
            sum += number
    val avg = sum.toDouble() / values.size
    return Pair(sum.toDouble(), avg)

Output :

Sum & Avg : (115.0, 28.75)

Benefits of a nested function

One of the primary advantages of nested functions is encapsulation. By nesting functions, you restrict access to the inner function, making it accessible only within the enclosing outer function.

This ensure that the inner function is not visible outside its scope, enhancing code modularity and preventing unintended use or modification.

Code Readability and Maintainability

Nested functions contribute significantly to code organization, excessive nesting should be avoided. Keeping functions concise and focused is crucial for readability. Additionally, It’s recommended to use nested functions when the inner function is relevant only within the scope of the outer function.

In Kotlin, nested functions provide an epic way to structure code, improving readability and maintainability. By organizing related functions and allowing access to the outer function’s variables, nested functions streamline code development

Nested functions can enhance code organization, but it’s important to avoid excessive nesting. Maintaining concise and focused functions is crucial for readability. Moreover, it’s advisable to employ nested functions specifically when the inner function pertains solely to the scope of the outer function.

You may also like...