Nested functions

Kotlin allows you to nest functions, one within another. We can declare and use a function within another function.

When you declare a function within another function, the nested functions, visibility will stay exclusively within the parent function and cannot be accessed from outside.

So, let's have an example:

fun main(args: Array<String>) { 
    fun nested():String { 
        return "String from nested function" 
    } 
    println("Nested Output: ${nested()}") 
} 

In the preceding program, we declared and used a function—nested()—inside the main function.

The following is the output, if you're curious:

So, as we've got our basics brushed up in functions, let's move forward in functional programming. In the following section, we will learn about function types.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.32.1