Middle
What functions are considered pure?
sobes.tech AI
Answer from AI
Pure function:
- Always returns the same result given the same input arguments.
- Has no side effects (does not modify global state, does not perform input/output, does not modify arguments passed by reference).
Examples:
<?php
// Pure function
function sum(int $a, int $b): int
{
return $a + $b;
}
// Impure function (modifies global state)
$counter = 0;
function incrementCounter(): void
{
global $counter;
$counter++;
}
// Impure function (performs output)
function printMessage(string $message): void
{
echo $message;
}