Block & Expression Body
Is there a difference really?
Yes in Kotlin
you can represent functions in more than one way.
In Block the function body is inside braces
{}
In Expressions the function body is declared as an expression
What about an example?
Let's see how we can declare a function that adds two numbers in more than one way.
Block
fun add( a:Int, b:Int) : Int {
return a + b
}
Notice the function body is inside braces
{}
.
Expression
fun add(a:Int, b:Int) = a + b
The function can be expressed in one line since the expression
a + b
has a value. The return type is also infered so you don't have to specify it.
Any benefits of this approach?
Before we go about benefits we should remember that
As developers we'll read more code than write
So wouldn't it be helpful if we could express a lot of information but with the fewest words possible? (I know you're nodding in agreement 😆😆)
Kotlin
aims at being concise, give a lot of information with few words.
Expression body functions do just that, you can declare what the function should do without a lot of boilerplate, hence making reading faster 😉 😉
- Expression body functions can only be used with expressions such as
if
,when
ortry
, they cannot be used with statements. - Removing the return type only works for functions with expression body, block body functions have to specify the return type and use the keyword
return
Check out other bits and try to guess if the functions have block or expression bodies, don't forget to add a reaction below or comment 😉 😉...