How Do We Pass Data?
When a function is called, it often needs input data to work on. But how does C actually give this data to the function? There are two main ways: Call by Value and Call by Reference.
Call by Value (Copy)
Passes a copy of the actual value. Changes inside the function do NOT affect the original variable.
Call by Reference (Address)
Passes the memory address of the variable. Changes inside the function modify the original variable directly.
Call by Value — The Photocopy
In C, parameters are passed by value by default. The function gets its own fresh memory locations (formal parameters) and copies the caller's values into them.
🔍 What happens in memory?
The variable x is a completely separate memory box. Modifying it doesn't affect num. They just temporarily shared the value 5 at the moment of the call.
Inside main: num = 5
The Swap Problem
A very common interview question: Write a function to swap two numbers. If you use Call by Value, it fails completely. Let's see why.
🚨 Why did it fail?
swap() makes copies of x & y into new variables a & b.a and b are successfully swapped locally.a & b are destroyed!Original
x & y were never touched.
Call by Reference (Using Pointers)
To modify the original variables, we don't pass copies of the values. We pass the memory addresses. We do this using Pointers.
The "Classic Rock" Analogy
Giving a function Call by Value is like sending a Screenshot of this playlist. They can draw over the photo, but your actual playlist never changes.
Giving a function Call by Reference (Pointer) is like sending the Collab Link. The function can follow the link directly to the source and add a new song!
The '&' Operator (Address-of)
Gets the physical memory address of a variable.
The '*' Operator (Dereference)
Goes to that memory address and accesses/modifies the value there.
Fixing Swap with Call by Reference
By passing addresses and using pointers, swap() can finally reach back into main()'s scope and swap the actual variables!
🔮 Visualizing Pointers
Because swap has the addresses (keys), when it writes to *pa, it actually reaches across memory and edits x inside main directly!
When to use Value vs Reference?
Choosing between Call by Value and Call by Reference depends entirely on what your function needs to achieve.
| Scenario | Approach | Why? | Example |
|---|---|---|---|
| Calculating a result from inputs | Call by Value | Safe! The function can't accidentally ruin the original variables. | pow(base, exp) |
| Modifying the original variables | Call by Reference | Required to send changes back to the caller's scope. | swap(&a, &b) |
| Returning MULTIPLE values | Call by Reference | return can only send back 1 value. Pointers allow modifying multiple outputs. |
getCoords(&x, &y) |
| Passing huge amounts of data (Structs/Arrays) | Call by Reference | Copying huge data is slow. Passing a tiny pointer is lightning fast. (Arrays are passed by reference automatically!) | processImage(imgPtr) |
Test Your Understanding
Let's see if you can differentiate between passing copies and passing addresses.