Side Effect
A side effect refers to any change in the state of a program or system that occurs when a function or expression is evaluated, beyond simply returning a value. Side effects are actions that affect something outside the function’s scope, such as modifying a global variable, reading or writing to a database, files or any remote API, displaying data, or modifying the environment.
Side effects are a concept of programming in general. It is important in functional programming, where functions without side effects are called pure.
Side effect has also the literal meaning of a consequence to an action or a situation, similar to collateral.
<?php
// A pure function, with no side effect
function trumpet($a, $b) {
return $a + $b;
}
// An impure function
function trample($a, $b) {
echo $a;
}
?>
See Also
Related
- Pure Function
- Functional Programming (FP)
- Determinism
- Event Sourcing
- Impure Function
- Effect System
- Fire-and-forget
- Global State
- Mutability