a - what is a pure function a pure function is a function that adheres to two primary principles: 1. Determinism → Given the same input, a pure function will always produce the same output. 2. No Side Effects → A pure function does not modify any external state or variables. Characteristics of Pure Functions: 1. Immutability → They do not change the input data. Instead, they work on a copy or produce new data. 2. Predictability → Since they have no side effects, their behavior is entirely predictable based solely on their inputs. 3. Testability → Because pure functions are independent of external state and always return the same result for given inputs, they are easier to test.
b - pure function example and benefits Example 1 → simple mathematical operation Example 2 → transforms an array without modifying the original Example 3 → generates a new object without modifying the input Example 4 → filters an array Benefits of Using Pure Functions: 1. Predictability → Easier to reason about and understand because they always produce the same output for given inputs. 2. Testability → Can be tested independently without setting up complex environments or mocking dependencies. 3. Concurrency → Safe to use in concurrent contexts since they don't depend on external state. 4. Reusability → Since they are self-contained, pure functions can be easily reused across different parts of an application.