Functional programming is a programming paradigm that focuses on the use of functions to solve problems. In functional programming, functions are treated as first-class citizens, meaning they can be assigned to variables, passed as arguments, and returned as values. Pure and impure functions are two important concepts in functional programming that play a crucial role in writing correct and efficient programs. In this article, we will discuss about pure and impure functions in JavaScript.
Pure Functions: A pure function is a function that always returns the same output when given the same input, and it does not have any side effects. A side effect is any modification that a function makes to the state of the system or its environment outside of its scope. A pure function is deterministic, meaning that its output is entirely determined by its input.
Pure functions have several benefits. They are easier to reason about and test because they do not have any hidden dependencies or states. They are also more flexible and reusable because they can be composed with other pure functions to create more complex behavior. Additionally, pure functions can be memoized, which means that their output can be cached for a given input, improving performance.
Here is an example of a pure function:
Javascript
function add(a, b) { return a + b; } console.log(add(3, 4)); |
7
This function takes two numbers as inputs and returns the sum of them. It is a pure function because it doesn’t modify any external state and always returns the same output for the same input.
Impure Functions: An impure function is a function that has side effects or does not always return the same output when given the same input. Side effects can include modifying a global variable, changing the state of an object, or making a network request.
Impure functions are harder to reason about and test because they have hidden dependencies and can produce different outputs for the same input. They are also less flexible and reusable because their behavior can be affected by external factors.
Here are some different examples of impure functions:
Example 1: Functions that modify a global variable
Javascript
let oldValue = 7; function add(newValue) { return oldValue += newValue; } console.log(add(5)); |
12
This function increments the global variable oldValue, which is outside of its scope. As a result, the oldValue variable is modified, and the function has a side effect.
Example 2: Functions that modify an object’s state
Javascript
const person = { name: "John" , age: 30, }; function incrementAge(person) { return ++person.age; } // person.age is now 31 console.log(incrementAge(person)); |
31
This function takes an object person as input and increments its age property. As a result, the state of the person object is modified, and the function has a side effect.
Example 3: Functions that generate random output
Javascript
function randomInt() { return Math.floor(Math.random() * 10); } console.log(randomInt()); |
3
This function generates a random integer between 0 and 9 each time it is called. As a result, the output of the function is not always the same for a given input.
Example 4: Functions that read input from the console
Javascript
function promptUser() { const name = prompt( "What is your name?" ); return `Hello, ${name}!`; } |
This function prompts the user for input using the prompt() function. As a result, the output of the function can be different each time it is called, depending on the user’s input.
These are just a few examples of impure functions. It’s essential to understand that impure functions can have side effects in many different ways, and their behavior can be affected by external factors. As a result, impure functions should be used only when necessary, and their side effects should be minimized as much as possible.
Conclusion: In conclusion, understanding the difference between pure and impure functions is crucial for building robust and maintainable software systems. Pure functions have no side effects and always return the same output for a given input, while impure functions can have side effects and produce different outputs for the same input. Pure functions are preferred in software development because they are easier to reason about, test, and compose with other functions. Impure functions should be used only when necessary, and their side effects should be minimized as much as possible.