Middle
In what cases can functions not be pure?
sobes.tech AI
Answer from AI
Functions cannot be pure if they:
- Have side effects: modify external state (e.g., change global variables, DOM, make server requests).
- Depend on external mutable data: if the function's result depends on state outside its parameters, it is not pure.
- Use random values or current time: return different results with the same inputs.
- Perform input/output operations (I/O), such as reading files or interacting with the user.
For example, a function that changes the component's state or calls an API cannot be pure. Pure functions are easier to test and predictable, so in functional programming, they are preferred to use as much as possible.