Functions

To define a function in Kotlin, you have to use the fun keyword, as follows:

fun firstClass() {
println("First class function")
}

The preceding snippet demonstrates that we can declare functions as first class citizens. We can also define functions as class members, as follows:

class A {
fun classMember() {
println("Class member")
}
}

A local function is a function that is declared in another one, as follows:

fun outer() {
fun local() {
println("Local")
}

local()
}

In the preceding snippet, the local function is declared inside of the outer function. The local functions are only available in the scope of a function where they were declared. This approach can be useful if we want to avoid duplicate code inside of a function.

This section will cover the following topics:

  • Functional programming
  • Higher-order functions
  • Lambdas
..................Content has been hidden....................

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