Local functions in Kotlin

In Kotlin, unlike Java, functions can include functions. These nested inner functions will have a local scope where they are defined as follows:

fun outerFunction(o : String) {
var outerFunctionVariable = 0;
fun innerFunction(i : String) {
outerFunctionVariable += 1;
println("Inner Function $i");
println("Outer Function Variable inside Inner Function
$outerFunctionVariable");
}

innerFunction(o);

println("Outer Function $o");
println("Outer Function Variable $outerFunctionVariable");
}

The outer function can be invoked as follows:

outerFunction("Hello World!");

An inner function can never be invoked from outside its scope but can be accessed from inside it. Also, outer function local variables can be used by inner functions but not vice versa. 

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

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