Junior — Middle
Can you explain what pure functions are in programming?
sobes.tech AI
Answer from AI
A pure function is a function that satisfies two main conditions:
- Determinism: it always returns the same result given the same inputs.
- No side effects: it does not modify the program's state or external data (for example, it does not change global variables, perform input/output).
Example of a pure function in PHP:
function add($a, $b) {
return $a + $b;
}
This function always returns the sum of its arguments and does not change anything outside itself.
Pure functions make testing, debugging, and parallel execution of code easier because their behavior is predictable and does not depend on external state.