NBC 103 • Unit 216 min readHigh Exam Frequency

Parameter Passing Mechanisms & Recursive Stack Architecture

Unit 2: Control Statements, Loops & Modular FunctionsProblem 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

4 Notebook Pages
NOTEBOOK PAGE 1 OF 4

1. Parameter Passing: Call by Value vs Call by Reference (via Pointers)

Call by Value (Default in C):

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!

Call by Reference (Simulated in C using Pointers):

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.

NOTEBOOK PAGE 2 OF 4

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.

NOTEBOOK PAGE 3 OF 4

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.

Stack Overflow:

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).

NOTEBOOK PAGE 4 OF 4

4. Classic Recursive Algorithms: Factorial & Euclidean GCD

Factorial Recurrence Relation:

Base case: $N! = 1$ when $N \le 1$

Recursive case: $N! = N \times (N-1)!$

Euclidean Greatest Common Divisor (GCD):

Base case: $\gcd(a, 0) = a$

Recursive case: $\gcd(a, b) = \gcd(b, a \pmod b)$

Comparative Architectural Matrix: Recursion vs Iteration (Loops)
ParameterRecursionIteration (Loops)
DefinitionFunction calls itself with smaller inputsA code block is repeatedly executed using loops
Termination ConditionSpecified by Base Case branchSpecified by loop boolean test condition
Memory OverheadHigh: Each call pushes a new stack frame onto RAMMinimal: Operates inside a single fixed stack frame
Execution SpeedSlower due to function call stack push/pop overheadFaster: Pure CPU jump instructions with zero call overhead
Code EleganceVery concise for trees, graphs, divide & conquerCan become lengthy and complex for nested structures
Infinite Failure ModeExhausts stack RAM leading to Stack Overflow crashCPU 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;
}
💡 Note:Demonstrates Call by Reference via address passing (&num1, &num2) to swap caller variables, and the recursive Euclidean GCD algorithm.

🎯 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.

Top Viva Questions on Parameter Passing Mechanisms & Recursive Stack Architecture

2 Questions
1

Why does C not have true Call by Reference like C++?

2

What is Tail Recursion and why is it desirable?