Lesson 10: Pass by value & scope
In the previous lesson we learned to pass values to a function — but what does the function really receive? In C every argument is passed by value: the function gets a copy, and changing the copy never touches the original. That's why an 'innocent' swap function that exchanges its parameters leaves
Passing a variable to a function is like sending a photocopy of a document: whoever gets the copy can scribble on it all they want — your original document stays perfectly clean.
- pass by value
- C's passing method: the function receives a copy of the value, not the variable itself.
- copy
- The value placed into the parameter at the call. Changing it does not affect the caller's variable.
- scope
- The region of code where a variable exists and is accessible — usually the block where it was defined.
- local variable
- A variable defined inside a function or block: born when the block starts, gone when it ends.
- global variable
- A variable defined outside all functions and visible to all of them. It exists, but is best avoided — it's hard to track who changes it.