Recursion

Ethan Taylor
5 min read
Listen to this study note
Study Guide Overview
This study guide covers recursion, including the base case and recursive calls. It explains the call stack and compares recursion to iteration, highlighting simplicity versus speed/memory trade-offs. Examples of iterative and recursive methods are provided. Finally, it demonstrates recursive traversals for ArrayLists, Strings, and arrays.
Recursion is a way to simplify problems by having a subproblem that calls itself repeatedly.
A recursive method has two parts: a base case and the recursive call. In the recursive call, the method basically calls itself, telling it to start over, either with the same parameters or different ones. After multiple calls, we approach the base case, where the recursion is stopped. Here is its anatomy:
public static void recursiveMethod()
if (baseCaseCondition) { // base case
base case steps
} else {
do something
recursiveMethod(); // recursive call
}
}
#The Base Case
The base case is the last recursive call. When the base case is reached, the recursion is stopped and a value is returned. The base case is the easiest part of the recursive call to write and should be written first to make sure that the program doesn't run forever.
#Recursive Calls and the Call Stack
Recursion involves a method calling itself with different parameter values until a base case is reached. The different recursive calls, or iterations of the method, are necessary to progress towards the base cas...

How are we doing?
Give us your feedback and let us know how we can improve