Pass-by-Value Semantics
What you'll learn: You'll understand how functions receive copies of arguments, not the originals, and why changes inside a function don't affect the caller's variables.
When you pass a variable to a function, most programming languages (like C, Java for primitives, and Python for immutable types) use pass-by-value semantics. This means the function receives a copy of the value, not the original variable itself.
How It Works
Think of it like photocopying a document. When you hand someone a photocopy of your essay, they can mark it up, highlight sections, or even tear it apart—but your original essay remains untouched. Similarly, when a function receives a copy of your argument, it can modify that copy freely without affecting the original variable in the caller's scope.
Example Scenario
function addTen(number):
number = number + 10
return number
x = 5
result = addTen(x)
// x is still 5
// result is 15
Even though addTen changes number inside the function, the original variable x remains 5. The function worked with a copy of 5, not the actual x variable.
Why This Matters
Pass-by-value creates isolation between functions. The caller's data is protected from accidental modification. If you want to change a value, the function must explicitly return the new value, and the caller must assign it back (like we did with result).
This predictability makes code easier to reason about—you know a function can't secretly change your variables unless you explicitly allow it through assignment.
Key Takeaway: Pass-by-value means functions receive copies of arguments; changes to parameters inside the function don't affect the original variables outside it.