What is functional programming, and how does it differ from imperative programming?

Functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

In more detail, functional programming is based on the concept of building software by composing pure functions, avoiding shared state, mutable data, and side-effects. It is declarative rather than imperative, and application state flows through pure functions. This is in contrast to the imperative programming style, which emphasises changes in state and the sequence of operations in time.

In functional programming, functions are first-class citizens. This means that functions can be passed as arguments to other functions, returned as values from other functions, and assigned to variables. This leads to a style of programming known as higher-order programming.

Another key aspect of functional programming is immutability. In functional languages, once a variable is set, it cannot be changed. This eliminates any changes in state, and makes the program much easier to follow and understand. It also leads to safer, more predictable code, and inherently makes your code more testable.

Functional programming also encourages recursion and the use of functions like map, filter and reduce. These functions can be used to operate on lists of data in a very clean and efficient way, without the need for loops and temporary variables.

In contrast, imperative programming is a programming paradigm that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how a program operates.

The main difference between the two is that while imperative programming is about how you want to achieve something, functional programming is more about what you want to achieve. In other words, in functional programming, you tell the computer what you want and let the computer figure out how to do it. In imperative programming, you tell the computer both what you want and how to do it.

Study and Practice for Free

Trusted by 100,000+ Students Worldwide

Achieve Top Grades in your Exams with our Free Resources.

Practice Questions, Study Notes, and Past Exam Papers for all Subjects!

Need help from an expert?

4.93/5 based on546 reviews

The world’s top online tutoring provider trusted by students, parents, and schools globally.

Related Computer Science a-level Answers

    Read All Answers
    Loading...