
Wikis & learning
Keep local and outer variables separate
A function can use a name that also appears outside it. Each parameter belongs to the current call and changing it usually leaves the caller's variable alone.
A function can use a name that also appears outside it. Each parameter belongs to the current call and changing it usually leaves the caller's variable alone.
Walkthrough
The returned number crosses the function boundary explicitly. Compare the value of the original variable before and after the call. Returning data makes a helper usable from a command-line script and a web page alike.
PHP example
<?php
function afterPacking(int $units): int {
$units -= 2;
return $units;
}
$units = 10;
echo afterPacking($units), ' remaining; originally ', $units;Expected output
8 remaining; originally 10Change and check
Call the function twice with the same starting value. Predict whether the second result will be different, then test it.
Discussion
Sign in to participate.



