Introduction to Functional Programming

Before we understand practical capacities of Scala language or attempt to learn core ideas of lambda expressions in Java, it is more crucial to understand the concept of Functional Programming first.

Most of the popular programming languages (like C, C++ and Java) are primarily created to follow the methodology of Imperative Programming, which implies that programmer generally write a code block (sequence of statements) which will change the state of the program(value of the variables) as each line is executed step by step.

Till Java 7 only Imperative paradigm is being followed by Java.

We normally use following constructs while working with Imperative paradigm in languages like Java & C++ by,

  • Using Assignments
  • Using Loops
  • Using Control Structures
  • And mainly, modifying the primitive variables and object states (which can be referred as Mutation)

Similar to imperative style of programming there is one more paradigm called Functional Programming which follows the declarative approach to solve any problem. It is more focused on WHAT is need to be solved rather that HOW it can be solved. And as name expresses functions are the basic building blocks for in this paradigm and are no different from integers, doubles and other data types. Programmer can pass function as a parameter and can return a function.

Let’s take a problem in order to get a closer look to both of these paradigms for broader understanding.

Problem: Moving from one place to another place in a Car

In Imperative paradigm, focus is on below steps

  • Sit down in the car
  • Start the engine
  • Press the accelerator
  • Control motion using accelerator and break
  • Follow the direction using steering wheel until destination
  • Stop the car

While working with Functional Programming, focus is mainly on following steps

  • Car is a vehicle which is used to move from one place to another place
  • Engine is the component which is responsible for moving the car
  • Steering wheel is the component which is used to control the direction of moving Car
  • Accelerator is the component which is responsible to move the car in forward and backward direction
  • Break is the component which is responsible to resist the motion of the Car

There are two ways to look at Functional programming,

General Functional Programming

This paradigm is function oriented but doesn’t have any restriction on mutation of variable and using assignments, loops and imperative control structures.

Restricted Functional Programming (Pure Functional Programming)

This function oriented paradigm is having restriction on mutable state of the variables and using assignments, loops and imperative control structures

While following Functional approach,

  • Programmer focus should be on WHAT rather than HOW.
  • Functions are the first-class objects rather than Classes and/or Instances
  • Primary focus is on function calls and recursion rather than loops and imperative control structures
  • Mutation of variables (State change of Variable) is restricted (for Pure Functional Programming)

Now the main question,

How to choose programming style among these two?

Answer is pretty simple, the way of choosing the programming style is mostly dependent on what type of evolution in code is expected.

Imperative style is more convenient when,

  • An application is require to have very limited number of operations on various items
  • And primary focus during any process or evolution is to add new items

Functional style is preferable when,

  • Application is limited with limited number of items
  • And primary focus is on adding new operations on existing items during any process or evolution


Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>