Kotlin Lambda Expressions

Kotlin Lambda Expressions are blocks of code that are self contained.

The Kotlin Lambda can have parameters defined to accept values, which are then assigned and operated upon to return a value.

val addValues= {firstValue: Int, secondValue: Int -> firstValue + secondValue}
val answer = addValues(100, 1)

You can return a value from a Lambda and assign it to a variable when using a function by declaring the variable like:

val answer = dateFunction()

You can also assign a reference to the function when declaring a variable by using the ‘::’ syntax. This then allows the function to be called by using the named variable:

val answer = ::dateFunction

Direct execution of a Lamba Block can be run directly using arguments in brackets at the end of the expression.

val answer= {firstValue: Int, secondValue: Int -> firstValue + secondValue}(100, 1)

The Kotlin Lamda expressions then help you to build tasks which can be reused within your code.