f(x)
Functions in C — Part 2
🏠 Home Lecture 17: Functions Basics
Lecture 17 · Functions Part 2

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.

If you want your friend to add a song to your playlist, what should you send them?
💾

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.

The Default Way

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.

call_by_value.cCopy Mechanism
void addTen(int x) { x = x + 10; // Only changes local copy! printf("Inside addTen: x = %d ", x); } int main() { int num = 5; addTen(num); printf("Inside main: num = %d ", num); return 0; }

🔍 What happens in memory?

main()
5
num
→
addTen()
15 5
x

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.

Output
Inside addTen: x = 15
Inside main: num = 5
A Classic Trap

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.

swap_fail.c❌ Fails!
void swap(int a, int b) { int temp = a; a = b; b = temp; // a and b are swapped locally! } int main() { int x = 10, y = 20; swap(x, y); printf("x=%d, y=%d ", x, y); return 0; }

🚨 Why did it fail?

Step 1
swap() makes copies of x & y into new variables a & b.
Step 2
a and b are successfully swapped locally.
💥
Function ends. a & b are destroyed!
Original x & y were never touched.
Output
x=10, y=20
The Solution

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.

Pink Floyd Led Zeppelin The Doors Nirvana Guns N' Roses AC/DC Coldplay Green Day Pink Floyd Led Zeppelin The Doors Nirvana Guns N' Roses AC/DC Coldplay Green Day
🎧

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.

int x = 10; printf("%p", &x); // prints address e.g. 0x7ffd
✨

The '*' Operator (Dereference)

Goes to that memory address and accesses/modifies the value there.

int *ptr = &x; *ptr = 50; // Changes x to 50!
Problem Solved

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!

swap_success.c✔️ Works!
// Expects pointers (addresses) as parameters void swap(int *pa, int *pb) { int temp = *pa; // read value at address pa *pa = *pb; // put value of pb into pa *pb = temp; // put temp into pb } int main() { int x = 10, y = 20; // Pass the ADDRESS of x and y swap(&x, &y); printf("x=%d, y=%d ", x, y); return 0; }

🔮 Visualizing Pointers

main() Memory
20
x
10
y
←
swap() Memory
*pa points to x
*pb points to y

Because swap has the addresses (keys), when it writes to *pa, it actually reaches across memory and edits x inside main directly!

Output
x=20, y=10
Decision Making

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)
💡 Pro Tip: Arrays in C are always passed by reference. When you pass an array to a function, you are actually passing a pointer to its first element!
Knowledge Check

Test Your Understanding

Let's see if you can differentiate between passing copies and passing addresses.

Question 1 of 5
Loading…