The Call Stack and Recursion

Introduction

Every time your program calls a function, the runtime has to remember where it was, what the local variables held, and where to send the return value. It does this with the call stack: a last-in, first-out structure of stack frames, one frame per active call. Recursion is where this bookkeeping becomes visible, because a single recursive function can have dozens or thousands of frames alive at once, each holding its own copy of the parameters and local state.

This topic matters in COP3530 because you cannot reason about recursive correctness or cost without a mental model of the stack. Tracing how frames are pushed on the way down to the base case and popped on the way back up is the same skill you will use later for tree traversals, divide-and-conquer sorting, and graph search. It is also where recursion fails: too much depth means stack overflow, and knowing why that happens tells you how to avoid it (a reachable base case, shallower recursion, memoization, or an iterative rewrite).

As you watch, keep a pen handy and draw the stack yourself. Look specifically for: what information a single frame stores, the exact moment the base case stops new frames from being created, the order in which frames resolve, and how the maximum depth relates to the input size. If a video uses Python or pseudocode rather than JavaScript, focus on the stack behavior — the mechanism is the same across languages.

Video 1: Lec-44: Understanding Call Stack with example | Data Structure #gatesmashers #Stack

Gate Smashers · 8:52 · 65,296 views · 3y ago · captions — Watch on YouTube

About this video

A lecture that builds up the concept of the call stack from first principles, using a worked example to show how function calls are pushed and popped as a program executes. Expect coverage of what a stack frame holds, how the return address and control flow are tracked, and the last-in-first-out ordering that governs which call finishes first.

Why this video

Gate Smashers is a widely used exam-prep channel for core CS topics, and this lecture gives you the foundational, language-neutral picture of the call stack that the other two videos assume — a direct match for the "What Is the Call Stack?" and "Stack Frames and Execution Context" subtopics.

Video 2: 13: How Recursion Works Internally | Call Stack Explained with Example

Amulya's Academy · 6:45 · 288 views · 2mo ago · captions — Watch on YouTube

About this video

This tutorial walks through a simple countdown function to show what happens internally when a function calls itself. Based on its description, it traces the call stack as each recursive call adds a frame, reaches the base case, and then unwinds as the frames return in reverse order.

Why this video

A short, example-driven treatment of exactly the subtopic "How Recursive Calls Build the Stack," and the countdown example is simple enough that you can replicate the trace by hand before attempting factorial or Fibonacci.

Video 3: 18: Python Recursion Depth Explained | Call Stack & Stack Frames

Amulya's Academy · 3:08 · 165 views · 1mo ago · captions — Watch on YouTube

About this video

A brief explainer on recursion depth, using factorial and Fibonacci as the running examples. It connects depth to the number of stack frames in play and discusses the limit that a language runtime places on how deep recursion may go before it fails.

Why this video

It covers "Stack Depth and Recursion Depth" and leads naturally into stack overflow, using the same classic functions you will implement in this module, so the depth analysis transfers directly to your own code.

How These Videos Support the Learning Outcomes

Together these videos give you the execution model behind the module outcomes. The call stack lecture and the countdown walkthrough let you trace frame-by-frame growth and resolution of a recursive call, which is precisely what MO3 asks for with factorial(3) or fibonacci(4). Seeing the base case act as the point where frames stop accumulating reinforces MO1's distinction between base and recursive cases and why a missing base case leads to overflow. The recursion depth video ties input size to stack consumption, supporting MO5's focus on the costs of naive recursion and on mitigation strategies, and giving you concrete evidence to weigh when deciding between recursion and iteration under MO4. The implementation work for MO2 happens in JavaScript in your own exercises, but the stack reasoning these videos build is what makes those implementations predictable rather than mysterious.

About These Videos

These videos were chosen from YouTube on October 2, 2026. Each has captions; the player turns them on. Videos on YouTube can be changed or removed — if one no longer plays, tell your instructor. If a player does not appear, use its Watch on YouTube link.