Theory
The swap that refuses to swap
Two toppers' marks got entered in the wrong order, and the program must swap x and y. Easy, you think, and write:
void swap(int a, int b) {
int t = a; a = b; b = t;
}
Call swap(x, y), print x and y... unchanged. The function ran, the logic is perfect, and nothing happened.
The most educational failure in C. Why it fails, and the one-character fix, is this whole lesson.
Theory
Photocopy vs house key
Hand a contractor a photocopy of your house plan and he can scribble anything on it, your house is untouched. That is pass by value: functions get copies. Hand him your address and keys, and his changes happen to the real rooms. That is pass by reference: functions get the address, and with it, the power to modify. The failing swap scribbled on photocopies.
Theory
The two modes, formally
Pass by value (C's default): parameters are fresh LOCAL copies of the argument values. swap's a and b were copies of x and y; the copies swapped, then died at return (last lesson's shredded drawer). x and y never knew.
Pass by reference (via pointers): pass ADDRESSES, receive them in pointer parameters, modify through dereference:
void swap(int *a, int *b) {
int t = *a; *a = *b; *b = t;
}
called as swap(&x, &y). Now *a IS x (two routes, one box, the pointers lesson), so the swap happens to the real thing.
Practical
The failing and the working swap, side by side
#include <stdio.h>
void swapCopy(int a, int b) { int t = a; a = b; b = t; }
void swapReal(int *a, int *b) { int t = *a; *a = *b; *b = t; }
int main() {
int x = 90, y = 55;
swapCopy(x, y);
printf("After swapCopy: x=%d y=%d\n", x, y);
swapReal(&x, &y);
printf("After swapReal: x=%d y=%d\n", x, y);
return 0;
}This example runs in Gri-Learn on the web, where you can edit it and see the output.
Think first
Predict both lines
Trace the code: swapCopy gets values 90 and 55; swapReal gets ADDRESSES. What do the two printf lines show?
Show the answer
After swapCopy: x=90 y=55 (copies swapped and died; originals untouched)
After swapReal: x=55 y=90 (the dereferenced swap reached the real boxes)
Same logic, one difference: what travelled into the function, values or addresses. This two-line trace IS the exam question on argument passing, year after year.
Quiz
Why does scanf("%d", &marks) demand the & while printf("%d", marks) does not?
- scanf must WRITE into marks, so it needs the address; printf only READS a copy of the value
- scanf is older syntax, printf is newer
- printf secretly adds the & itself
- It is an arbitrary inconsistency in C
Show the answer
scanf must WRITE into marks, so it needs the address; printf only READS a copy of the value
printf consumes a value: a photocopy serves fine. scanf's whole job is DELIVERING input into your variable, output-parameter work, so it needs the key (address). The full circle: Unit 1's mysterious &, the pointers unit's explanation, and today's vocabulary, printf passes by value, scanf needs a reference.
Quiz
A function receives an array: void curve(int marks[], int n), and adds 5 to every element. After the call, the caller's array is...
- Changed: an array passes as the ADDRESS of its first element
- Unchanged: arrays are copied like ints
- Changed only if the function returns it
- Deleted: arrays cannot enter functions
Show the answer
Changed: an array passes as the ADDRESS of its first element
The array NAME is an address (the signpost lesson), so what enters the function is a pointer to the caller's real boxes: modifications land on the originals, no & needed. Arrays are effectively always pass-by-reference, and copying sixty boxes would be wasteful anyway. This is why grace-marks functions in lab exams just work.
Watch out
Where marks leak
The failing swap written as the answer: values swap inside, originals untouched, know both versions cold. Forgetting & at the call: swapReal(x, y) hands VALUES where addresses are expected, compilers warn loudly. The honest footnote for toppers: technically C is ALWAYS pass-by-value, a pointer is an address passed by value; "pass by reference" is the working name for the pointer technique. Writing that nuance earns respect, missing it loses nothing at BCA level.
Theory
When to reach for which
Inputs the function only reads: by value (safety: the caller's data cannot be damaged). Outputs, multiple results (the total-percentage-grade problem from last lesson), or big arrays: by reference. One value out = return; many values out = addresses in. Two lessons remain in C: a function that calls ITSELF, and the struct that finally gives our student a single record.
Summary
Key takeaways
- Pass by value (default): parameters are copies; the caller's variables are safe and unchangeable.
- Pass by reference (pointers): pass &x into int a; a modifies the real variable.
- The classic proof: swap(int,int) fails; swap(int,int) with &x, &y works.
- scanf needs & (writes through the address); printf reads a value copy.
- Arrays pass as the address of element zero: functions modify the original.
- One result = return; many results = pass addresses.
- Memory hook: photocopy vs house key.