Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 2 of 8
Call by Value - What the Stack Actually Does
C passes ALL arguments **by value** - the function receives a **copy** of the value, never the original. This means a function cannot modify a caller's local variable unless given a **pointer** to it. Understanding why `swap(a, b)` doesn't work - and how to fix it - is the gateway to understanding pointers.
Call by Value - Stack Frames Visualised
#include <stdio.h>
/* ── Why 'swap' doesn't work by value ───────────────────── */
void swap_broken(int a, int b) {
/* a and b are COPIES on a NEW stack frame.
Swapping them has zero effect on caller's variables. */
int tmp = a;
a = b;
b = tmp;
printf("Inside swap_broken: a=%d, b=%d
", a, b);
/* Stack at this point:
main frame: [x=5] [y=10] ← unaffected
swap_broken frame: [a=10] [b=5] [tmp=5] ← temporary */
}
/* ── The fix: pass POINTERS to the variables ───────────── */
void swap_correct(int *a, int *b) {
/* a and b are ADDRESSES of the caller's variables.
*a dereferences the pointer to read/write the value. */
int tmp = *a;
*a = *b;
*b = tmp;
/* Stack at this point:
main frame: [x=?] [y=?] ← BEING MODIFIED via pointers
swap_correct frame:[a=&x] [b=&y] [tmp=old *a] */
}
void modify_value(int n) {
n = 99; /* modifies local copy only */
}
void modify_via_pointer(int *p) {
*p = 99; /* modifies the value at the address given */
}
int main(void) {
int x = 5, y = 10;
/* Call by value - x, y unchanged */
swap_broken(x, y);
printf("After swap_broken: x=%d, y=%d
", x, y); /* 5 10 */
/* Call via pointers - x, y ARE swapped */
swap_correct(&x, &y);
printf("After swap_correct: x=%d, y=%d
", x, y); /* 10 5 */
int val = 42;
modify_value(val);
printf("After modify_value: %d
", val); /* 42 - unchanged */
modify_via_pointer(&val);
printf("After modify_via_pointer: %d
", val); /* 99 - changed */
return 0;
}
/* ── The call stack - memory layout during a call ─────────
Stack grows DOWNWARD on most architectures.
High address
┌──────────────────┐
│ OS / kernel │
├──────────────────┤
│ main's frame │ x=5, y=10, val=42
├──────────────────┤ ← stack pointer before call
│ swap's frame │ a=copy, b=copy, tmp
├──────────────────┤ ← stack pointer inside swap
│ (grows down) │
└──────────────────┘
Low address (heap and data segments below) */Common Mistakes
- Expecting a function to modify a caller's variable without pointers - `void double_it(int n) { n *= 2; }` then `double_it(x)` - x is unchanged. Pass `&x` and use `*n *= 2` in the function.
- Passing a large struct by value - structs are value-copied when passed. A 1 MB struct passed to 100 nested functions creates 100 MB of stack copies. For large structs, always pass a pointer: `void process(const MyStruct *s)`.
- Returning a pointer to a local variable - `int *f(void) { int x = 5; return &x; }` - `x` lives on `f`'s stack frame which is gone when `f` returns. The pointer is dangling. This is undefined behaviour.
Tip
Tip
Practice Call by Value What the Stack Actually Does in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
C passes parameters strictly by value, copying values onto the new activation stack frame
Practice Task
Note
Practice Task - (1) Write a working example of Call by Value What the Stack Actually Does from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with Call by Value What the Stack Actually Does is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- C passes ALL arguments **by value** - the function receives a **copy** of the value, never the original.
- Expecting a function to modify a caller's variable without pointers - `void double_it(int n) { n *= 2; }` then `double_it(x)` - x is unchanged. Pass `&x` and use `*n *= 2` in the function.
- Passing a large struct by value - structs are value-copied when passed. A 1 MB struct passed to 100 nested functions creates 100 MB of stack copies. For large structs, always pass a pointer: `void process(const MyStruct *s)`.
- Returning a pointer to a local variable - `int *f(void) { int x = 5; return &x; }` - `x` lives on `f`'s stack frame which is gone when `f` returns. The pointer is dangling. This is undefined behaviour.
Finished this lesson?