October 2018
Intermediate to advanced
370 pages
9h 15m
English
In Kotlin, a function can behave as an expression. Let's take the example of the add function from the previous section and convert it into an expression:
fun addValues(i: Int, j: Int): Int { return i + j }
This function takes two parameters, adds them, and returns an integer value. When the function contains only one line of code, it can be written as an expression.
Create a new function as an expression:
fun addValuesEx(a : Int, b : Int) : Int = a + b
The explicit declaration of the function return type can be removed as follows:
fun addValuesEx(a : Int, b : Int) = a + b
Now add an equals operator, remove the curly brackets, and remove the explicit type declaration of the return type along with the return keyword ...
Read now
Unlock full access