Understanding Functional Programming with Scala

Introduction
Functional languages are those which treat functions as first class citizens. You can identify the validity of first class citizen status for function by checking the following aspects:
1. Can you assign a function to a value or a variable
2. Can you return a function from another function
3. Can you pass function as a parameter to another function
Scala supports both Object Oriented as well as Functional Programming. Besides treating functions as first class citizens, Scala provides additional features for working with functions:
1. Closures: retain referencing environment
2. Parameter groups: logical groups of parameters to a function
3. Partial Application: Fix some but not all parameters
4. Currying: Fix some but not all parameter groups
Why Scala?
Scala runs on JVM so you would ask what is the need for Scala when we have the all so popular language Java running on JVM?
Nowadays with exponential rise of data, distributed computing has become a necessity. Functional languages have the following features which enable them to work well with distributed computing:
1. Functions are first class citizens which means they can be passed as parameter or returned as response from other functions
2. They provide special notation to create a sequence of calling functions like with the dot notation in Java 8
3. They support data immutability which prevents accidental data updates. Prefix the name of the data holder with ‘val’ and it will be treated as immutable. To declare it as a variable whose value is allowed to change, use the prefix ‘var’. The common collections in Scala namely List, Map and Set are also immutable.
4. Functional languages promotes pure functions i.e. same output on same input irrespective of the number of times the function is called.
5. Less risky to design concurrent computations working with pure functions on immutable data as immutable data is inherently thread-safe
Expressions vs Statements
Functional programming in general prefer the use of expressions over statements because expressions can be used as function arguments and can also replace functions. Simply put, functions are named expressions although there is a concept of anonymous functions as well. Also unlike functions, expressions do not have an argument passed.
A very common mistake people make is in differentiating an expression from a statement so lets take a look at some of the key differences between them:
1. Expressions are block of code returning value whereas statements do not return any value. An easy way to figure that out is that the expressions can be used on the right side of assignment operator. It means even creating a collection is expression as it returns an object with its element values but if we assign it to a variable it becomes a statement as assignment doesn’t return any data or value to further work upon
2. Expressions can be chained unlike statements as they dont have a return value which can be fed as input to a next chained statement
3. Expressions can be used as parameter as well as return value of a function
In Scala, the syntax for block of expression is enclosed within curly braces {}.
Many conditional and control statements are also expressions in Scala e.g. if/else, for loop with yield keyword or match statements. Read more about them in the Scala Basics blog.