Statements and Expressions in Scala

If you know programming in any language, you already know about statements and expressions.
A statement is the smallest standalone element that expresses some action to be carried out.
Whereas an expression in a programming language is something that produces or returns a value.
Here is an example.

With this information, We can say that a program is nothing but a sequence of statements and expressions.
Let's look at an example.

If you carefully look at this small program, you can extend the definition of a program. I can now say that a program is nothing but a sequence of statements and expressions that modify some program state.
In this program, we have an if statement and then we have a print statement. Both of these statements are modifying something. The if statement is changing the state of r and print statement changes the state of the console.
All the I talked so far about statements and expressions is applicable to imperative programming. In the Functional Programming model, every functional programming statement should have a capability to return a value. In other words, you can say that we do not have statements in functional programming. We only have expressions. That is the ground rule for functional statements. This rule is even valid for print statement.
The example below proves that a println returns a value.

So, the println function returns a unit. The unit is like a void in Java programming. However, it is not exactly same as void. The void means nothing whereas unit has a value. We represent the unit value using () symbol.
So, the point that I wanted to make is that every statement in Scala can return some value. Even a Scala loop and println can return a value. Since a Scala statement returns a value, some people do not call them a statement. They call them an expression. So, it is common that you find people saying that Scala does not have statements but only expressions.
You might be wondering about the benefits of a statement returning a value. I mean, we learned that functions return a value but why do we want every statement to return a value. What is the benefit?

Benefits of Expressions over Statements

When you start practicing Functional Programming approach, you will realize that using functional statements allows us to reduce the number of variables in our code. Removing the variables from your code helps you to achieve immutability. I mean, If you do not have a variable, you do not need to mutate it. Let's come back to the myResult function. The code that we listed earlier is imperative. Let's make a functional version of the same code.

The if statement returns a value hence we do not need a variable. The functional version of the code is concise, and the returning statement helps us to eliminate variables and achieve immutability.
Keep reading for more interesting functional concepts.

Read More

Pure Functions | Referential Transparency | Benefits of pure functions | First class functions | Higher order function | Anonymous functions | Immutability | Tail Recursion | Expressions in Scala | Lazy Evaluations | Pattern Matching | Closures

By Prashant Pandey -

You will also like: