The right-hand side of a lambda expression can be written in two ways:
- Expression notation: the right-hand side is as an expression, such as in
(a, b) → a + b
- Block notation: the right-hand side is a conventional function body with a code block and an optional return statement, such as in
(a, b)
→ {return a + b;}
By convention, expression notation is preferred over block notation. Block notation must be used when the function implementation requires more
than one statement. However, when the code block consists of only one statement (which may or may not be a return
statement), it can be
rewritten using expression notation.
This convention exists because expression notation has a cleaner, more concise, functional programming style and is regarded as more readable.