Parameter Passing Mechanisms & Recursive Stack Architecture
Unit 2: Control Statements, Loops & Modular Functions • Problem Solving Using C
👨🏫 Professor's Mental Model: Photocopy Handout vs Master Key & Russian Nesting Dolls
Call by Value ek book ki photocopy baantne jaisa hai: agar student photocopy par kuch likh bhi de, toh teacher ki original book par koi asar nahi padega! Call by Reference original cupboard ki chabi (memory address) dene jaisa hai: andar koi bhi badlaav seedha original saman par hoga. Aur Recursion Russian Matryoshka doll jaisa hai: badi doll ke andar waisi hi choti doll khulti hai jab tak sabse aakhri Base Doll na mil jaye!
Academic Lecture Notes & Solved Study Pages
Unit 2 • Core Concepts, Step-by-Step Proofs & Notebook Solutions
1. Parameter Passing: Call by Value vs Call by Reference (via Pointers)
A duplicate bitwise copy of the actual argument value is passed to the formal parameter.
Any changes made to the formal parameter inside the function are confined to that local stack frame and DO NOT reflect in the caller's variable!
Instead of values, the memory addresses (`&x`, `&y`) of the caller's variables are passed.
The function receives pointer variables (`int *a`, `int *b`) and dereferences them (`*a`, `*b`) to modify the caller's memory directly.
2. The Call Stack & Activation Record Frames
When a function is called, the CPU allocates an Activation Record (Stack Frame) on top of the Call Stack in RAM containing: 1. Incoming arguments and formal parameters. 2. Local variables declared inside the function. 3. Return memory address (where to resume execution after function finishes). 4. Previous frame pointer. When the function completes, its stack frame is automatically popped (deallocated), restoring the caller's frame.
3. Recursion: The 2 Mandatory Foundations
Recursion is a programming technique where a function calls itself directly or indirectly to solve a smaller instance of the same problem.
Every mathematically valid recursive function MUST contain: 1. Base Case (Stopping Condition): A non-recursive terminal branch that stops the recursion and returns a known constant value. 2. Recursive Step (Inductive Step): Where the function calls itself with modified parameters that progressively converge toward the base case.
If the base case is omitted or incorrect, the function calls itself infinitely, continually allocating stack frames until RAM stack memory is exhausted, triggering an immediate crash (Segmentation Fault).
4. Classic Recursive Algorithms: Factorial & Euclidean GCD
Base case: $N! = 1$ when $N \le 1$
Recursive case: $N! = N \times (N-1)!$
Base case: $\gcd(a, 0) = a$
Recursive case: $\gcd(a, b) = \gcd(b, a \pmod b)$
| Parameter | Recursion | Iteration (Loops) |
|---|---|---|
| Definition | Function calls itself with smaller inputs | A code block is repeatedly executed using loops |
| Termination Condition | Specified by Base Case branch | Specified by loop boolean test condition |
| Memory Overhead | High: Each call pushes a new stack frame onto RAM | Minimal: Operates inside a single fixed stack frame |
| Execution Speed | Slower due to function call stack push/pop overhead | Faster: Pure CPU jump instructions with zero call overhead |
| Code Elegance | Very concise for trees, graphs, divide & conquer | Can become lengthy and complex for nested structures |
| Infinite Failure Mode | Exhausts stack RAM leading to Stack Overflow crash | CPU hangs indefinitely in an infinite loop without crashing |
Interactive Tested Code Example
#include <stdio.h>
// 1. Call by Reference Simulation via Pointers
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
// 2. Recursive Euclidean GCD Algorithm
int gcd(int a, int b) {
if (b == 0) return a; // Base case
return gcd(b, a % b); // Recursive step
}
int main(void) {
int num1 = 48, num2 = 18;
printf("GCD of %d and %d = %d\n\n", num1, num2, gcd(num1, num2));
printf("Before Swapping: num1 = %d, num2 = %d\n", num1, num2);
swap(&num1, &num2); // Passing memory addresses
printf("After Swapping: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}🎯 University Exam Scoring Blueprint
- Always draw the recursive stack activation trace showing frame push order and unwinding return values.
- In Call by Value vs Reference questions, write a 4-line swap() code snippet showing why value swapping fails without pointers.
- Emphasize that Stack Overflow occurs when recursive depth exceeds available stack segment space.