February 2018
Intermediate to advanced
350 pages
7h 35m
English
Kotlin also allows us to have functions as properties. Functions as properties means that a function can be used as a property.
For instance, take the following example:
fun main(args: Array<String>) {
val sum = { x: Int, y: Int -> x + y }
println("Sum ${sum(10,13)}")
println("Sum ${sum(50,68)}")
}
In the preceding program, we created a property, sum, which will actually hold a function to add two numbers passed to it.
While sum is a val property, what it holds is a function (or lambda) and we can call that function just like the usual function we call; there are no differences there at all.
If you're curious, the following is the output:
Now, let us discuss the syntax of lambda.
In Kotlin, a lambda always stays embraced ...
Read now
Unlock full access