0% found this document useful (0 votes)
4 views

Algorithms_ Recursion Cheatsheet _ Codecademy

This document is a cheatsheet on recursion, outlining its fundamental components: the base case and the recursive step. It explains the importance of a base case to prevent infinite recursion and discusses the call stack, big-O runtime, and execution context of recursive functions. Additionally, it highlights the risks of weak base cases leading to stack overflow errors.

Uploaded by

zoya bari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Algorithms_ Recursion Cheatsheet _ Codecademy

This document is a cheatsheet on recursion, outlining its fundamental components: the base case and the recursive step. It explains the importance of a base case to prevent infinite recursion and discusses the call stack, big-O runtime, and execution context of recursive functions. Additionally, it highlights the risks of weak base cases leading to stack overflow errors.

Uploaded by

zoya bari
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

5/1/25, 2:11 PM Algorithms: Recursion Cheatsheet | Codecademy

Cheatsheets / Algorithms

Recursion

Base Case of a Recursive Function

A recursive function should have a base case with a function countdown(value)


condition that stops the function from recursing
if value is negative or zero
indefinitely. In the example, the base case is a condition
evaluating a negative or zero value to be true. print "done"
otherwise if value is greater than zero
print value
call countdown with (value-1)

Recursive Step in Recursive Function

A recursive function should have a recursive step def countdown(value):


which calls the recursive function with some input that
if value <= 0:
brings it closer to its base case. In the example, the
recursive step is the call to countdown() with a print("done")
decremented value. else:
print(value)
countdown(value-1) #recursive step

What is Recursion

Recursion is a strategy for solving problems by defining


the problem in terms of itself. A recursive function
consists of two basic parts: the base case and the
recursive step.

https://www.codecademy.com/learn/fscj-22-algorithms/modules/wdcp-22-recursion-d930f071-374e-444b-b8d1-f6229c2c3735/cheatsheet 1/3
5/1/25, 2:11 PM Algorithms: Recursion Cheatsheet | Codecademy

Call Stack in Recursive Function

Programming languages use a facility called a call stack


to manage the invocation of recursive functions. Like a
stack, a call stack for a recursive function calls the last
function in its stack when the base case is met.

Big-O Runtime for Recursive Functions

The big-O runtime for a recursive function is equivalent


to the number of recursive function calls. This value
varies depending on the complexity of the algorithm of
the recursive function. For example, a recursive
function of input N that is called N times will have a
runtime of O(N). On the other hand, a recursive
function of input N that calls itself twice per function
may have a runtime of O(2^N).

Weak Base Case in Recursive Function

A recursive function with a weak base case will not have


a condition that will stop the function from recursing,
causing the function to run indefinitely. When this
happens, the call stack will overflow and the program
will generate a stack overflow error.

https://www.codecademy.com/learn/fscj-22-algorithms/modules/wdcp-22-recursion-d930f071-374e-444b-b8d1-f6229c2c3735/cheatsheet 2/3
5/1/25, 2:11 PM Algorithms: Recursion Cheatsheet | Codecademy

Execution Context of a Recursive Function

An execution context of a recursive function is the set


of arguments to the recursive function call.
Programming languages use execution contexts to
manage recursive functions.

Print Share

https://www.codecademy.com/learn/fscj-22-algorithms/modules/wdcp-22-recursion-d930f071-374e-444b-b8d1-f6229c2c3735/cheatsheet 3/3

You might also like