Functional Programming: Pure and Impure Functions
Last Updated :
28 Apr, 2025
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));
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));
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));
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());
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.
Similar Reads
Functional programming in JavaScript Functions are the most important part of functional programming (especially in JavaScript). Functions are the single source that helps developers to perform functional programming. Generally abbreviated as FP which revolves around functions and it is how we use functions that makes our code function
7 min read
Functional Programming in JavaScript Functional Programming(FP) is as old as programming but some of us think that it is discovered recently because we found some parts in mathematics but not in the programming world. But nowadays functional programming is in trend. Almost every programming language including Java, Python, JavaScript,
4 min read
PHP get_defined_functions() Function The get_defined_functions() function is an inbuilt function in PHP which returns the all defined functions in the array format. Syntax: array get_defined_functions( bool $exclude_disabled = true )Parameters: This function accepts one parameter that is described below: $exclude_disabled: It will chec
2 min read
Explain the concepts of functional programming in JavaScript Every program we write follows an approach or style of writing also referred to as a paradigm. Functional programming is a declarative programming paradigm (programming paradigm where we write code in such a way that it describes the result and not the approach). To understand functional programming
2 min read
What is a pure functional component in ReactJS ? What is a JavaScript function? A JavaScript function is a block of code designed to perform a particular task which is executed when it is called. How React functional components are similar to JavaScript functions? Conceptually, components are like JavaScript functions. Â A functional component is a
3 min read
TypeScript Anonymous Functions Type In TypeScript, an Anonymous Function Type defines a function without a specific name, specifying parameters and return types. This allows for flexible and reusable function definitions, enabling the assignment of functions to variables and the use of type annotations for parameters and return values
3 min read
Understanding the Difference between Pure and Impure Functions in JavaScript In this article, we will see the concepts of pure and impure functions in JavaScript, along with understanding their differences, & basic implementation for a better understanding of the concepts. Pure Functions: This function always returns the same output as given the same input parameters. Pu
4 min read
Pure Functions in JavaScript A Pure Function is a function (a block of code) that always returns the same result if the same arguments are passed. Pure functions return consistent results for identical inputs.They do not modify external states or depend on mutable data.Often used with immutable data structures to ensure reliabi
2 min read
TypeScript Assignability of Functions In this article, we will explore the concept of assignability of functions in TypeScript. Specifically, we will discuss how functions with a return type of void can be assigned to function types with other return types, including those that return values.Understanding AssignabilityIn TypeScript, fun
3 min read
How to create a Functional Component in React? To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
2 min read