Skip to main content

Functional Programming: Introduction

What is Functional Programming? A simple Introduction.

Updated Markdown
Part of seriesFunctional Programming
Functional Programming: Introduction

Assalam u Alaikum & Hello Everyone! πŸ‘‹

I hope you all are good βœ¨πŸ€—

I'm back with another series full of learning i.e Functional Programming πŸš€

In this article, we are going to learn What is functional programming and what we are going to learn in this series.

Contents

  • ⚑ Imperative Programming
  • ⚑ Declarative Programming
  • ⚑ Imperative vs Declarative Programming.
  • ⚑ Now, What is Functional Programming.

Let's start πŸš€

Functional Programming is another Programming Paradigm just like Object-Oriented Programming. There are many small bits and pieces which we have to understand before understanding what is functional programming. The most important concept that is the prerequisite to understanding FP, is the understanding of Imperative & Declarative Programming

Imperative Programming

Imperative programming is a programming paradigm in which the program is written in a structured manner. Imperative programming describes the computer "How" to accomplish a specific task.

Procedural & OOP both paradigms are of Imperative Programming. We explicitly tell how we want to accomplish a specific task.

Example

Suppose that, we have a scenario in which we have to write a function that accepts an array of integers and returns all the integers that are less than 10.

Here is a way to solve this problem in an Imperative way:

const filterLessThan10 = (arr) => {
  // 1. create a new array
  const result = [];
  // 2. loop over tha array
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] < 10) {
      // 3. if the item is less than 10, push it into result
      result.push(arr[i]);
    }
  }
  // 4. return result
  return result;
};

const numbers = [1, 2, 10, 42, 4, 98, 23, 92, 9, 2, 7];
console.log(filterLessThan10(numbers)); // [1, 2, 4, 9, 2, 7]

As you can see, in this program, we are explicitly telling the steps that a computer has to perform. In technical terms, there is No Abstraction in the function filterLessThan10. We are completely aware of how the function works internally.

Declarative Programming

Declarative programming is a programming paradigm in which the programmer defines what needs to be accomplished by the program without defining how it needs to be implemented.

In simple words, we don't tell the computer how to achieve a task, instead, we just command the computer to perform this task with zero interest in how the computer will perform it.

Prolog, SQL, and embedded SQL are some of the well-known examples of declarative programming languages.

Example:

We will refactor our filterLessThan10 and write it in a declarative manner.

const filterLessThan10 = (arr) => {
  return arr.filter((item) => item < 10);
};

const numbers = [1, 2, 10, 42, 4, 98, 23, 92, 9, 2, 7];
console.log(filterLessThan10(numbers)); // [1, 2, 4, 9, 2, 7]

As you can see, we have called the built-in filter method. We don't know how the filter is implemented. In technical terms, the filter method is an Abstraction and we are hiding all the internal details. We are just providing the filter criteria without the looping, pushing, and returning details.

Declarative vs Imperative Programming

Imperative ProgrammingDeclarative programming
It uses statements that change a program’s state.It expresses the logic of a computation without describing its control flow.
Imperative Programming is like your friend calling your father and telling him how to fix your car step by step.Declarative Programming is like asking your friend to fix your car. You don’t care how to fix it, that’s up to him.
We do not hide the details of a task how it's performed (Though we can use Abstraction which is an OOP principle)By Default Abstraction, The internal details are hidden among the tasks.

Now, What Is Functional Programming?

carbon.png

Functional Programming is a programming paradigm that follows the declarative paradigm approaches to perform computational tasks. In functional programming, the programming is done with expressions or declarations instead of statements.

In functional programming, we break a big task into many chunks (pure functions) and perform the tasks by calling them accordingly. The term functional programming has been derived from the mathematical functions.

In mathematics, we often see something like:f(x) = y which means a function f will take an input x and will produce some result y. The idea is the same in the world of functional programming as well.

Keeping all this in mind, we can describe functional programming as:

Functional programming (often abbreviated FP) is the process of building software by composing pure functions, avoiding shared state, mutable data, and side-effects.

What We Will Learn?

These are the topics which we are going to learn in this series in detail. Every topic will contain multiple small exercises and real-world use cases and by the end of it, you'll be confident in functional programming.

  • Advantages & Applications of FP
  • Immutability
  • Pure Functions & Side Effects
  • Higher-Order Functions
  • Function Composition

That's it, folks! hope it was a good read for you. Thank you! ✨

πŸ‘‰ Follow me: Github Twitter LinkedIn Youtube

About the author

  • A Full Stack Software Engineer(JavaScript) who’s passionate about creating content that can be of value to others. Besides juggling work, content creation and other commitments, I can be found traveling to contemplate the meaning of life under the stars 🌠

Man this series is what I need :D

8

Thank you soo much πŸš€βœ¨

Great explanation Rehan Sattar. I'm a big fan of the paradigm Functional Programming.

8

The more you do FP, the more you fall in love with it!

2

Can we code every app successfully using functional programming as we do with OOP paradigm?

5

I believe it's possible. The better approach to use functional programming is to build applications in modules that are small and independent. It helps to build software that is easy to maintain and scale.

Also, there are many languages such as Clojure which are functional languages by default, and tons of applications are built using such languages.

3

Great, waiting for the next one.

14

Thanks, coming soon 🀩

Functional Programming

Part 4 of 4

In this series, we will learn about Functional Programming paradigm. What it is, how to do funtional programmin, how to compose different functions with some real world examples with illustrations.

Start from the beginningFunctional Programming: Higher Order FunctionsA simple guide to understanding Higher Order Functions in the world of Functional Programming with Examples in JavaScript.