Functional programming
Functional programming is one of the programming paradigms that is based on the following principles:
-
- Functions are first-class objects, first-class citizens. This means that variables can be assigned just like other objects and primitive values; functions can be used as arguments to other functions or as their return values.
- Functions operate on immutable data structures. Data structures in functional programming are typically immutable or unchangeable. Operations performed on data structures, however, create new data structures as needed and return them as results. For example, in purely functional programming languages, once lists or other data structures are created, they cannot be modified.
- Functions have no side effects. In functional programming, functions typically have no side effects at all and behave more like mathematical functions. This means that functions in functional programming always return the same result for the same input and do not cause any side effects. In purely functional languages, side effects are prevented by the language itself.
- Functional programs employ a declarative programming style. Imperative programming is a programming paradigm in which the computer is given very precise, individual instructions on how to solve a problem. Imperative programs use explicit loop statements (while, for, and so on), conditional statements (if-else), and sequences of them. Functional programs, on the other hand, employ a declarative style, meaning the program specifies what to do rather than how to do it. As a result, functional programs are typically more readable, more meaningful, and more compact than equivalent imperative code.

Various languages may not be purely functional, but they can still support the functional paradigm. For example, consider JavaScript, which isn’t a purely functional language but allows for functional programming. Consider the following JavaScript array iteration program:
|
1
2
3
4
5
6
7
8
9
10
11
12
|
set of objects to iterate over// const people = [ {name: "Tom", age:38}, {name: "Kate", age:31}, {name: "Bob", age:42}, {name: "Alice", age:34}, {name: "Sam", age:25} ]; iterating over array objects// for(let i = 0; i < people.length; i++){ console.log(people[i].name);} |
This code is intended to print each array value. That’s what should happen. However, the code is actually more focused on how it should all work: initialize the counter variable, check the termination condition, access the array element at a specific index, and then increment the counter variable.
Thus, imperative programs contain a lot of unnecessary code (boilerplate), which reduces program readability and increases program bloat. Of course, if we’re talking about the code above, which is extremely simple and easy to navigate, then if you encounter deeply nested loops and conditional statements, you can get lost in the program.
Using functional programming, problems can often be formulated in a much more readable manner. For example, the forEach() method, which iterates through an array and receives a callback function as a parameter, is much more suitable for this task. The callback function receives the current array element being iterated over and performs certain actions on it:
|
1
2
3
4
5
6
7
8
9
10
|
set of objects to iterate over//const people = [ {name: "Tom", age:38}, {name: "Kate", age:31}, {name: "Bob", age:42}, {name: "Alice", age:34}, {name: "Sam", age:25} ];const printName = (p)=>console.log(p.name);people.forEach(printName); |
While functional code isn’t necessarily shorter in terms of lines, it’s much more readable. Unlike imperative code, the focus is now on the program logic ( what the program does) rather than the loop itself ( how it does it).