Skip to main content

Section 9.2 Tracing Recursive Code

When writing a recursive function, it is best to approach it from a top-down perspective. Instead of worrying about how the recursive call solves the sub-problem, trust that it will provide the correct solution. Treat the result of the recursive call as if it were a reliable library function that helps solve the original problem accurately.
However, when you need to read or trace a recursive function, it is essential to consider how the function works. Tracing a few recursive functions is an excellent way to understand the behavior of recursion. As you become more familiar with tracing, you will find that you rarely need to delve into all the details. You will gain confidence in your understanding of recursion.
Remember that information can be passed from one recursive call to another through function parameters. This happens on increasingly smaller problems until a base case is reached during the winding phase. Then, as the series of recursive calls unwinds, a return value is passed back. It’s important not to overlook the unwinding phase, as it completes the recursive process.
Here is a link to the OpenDSA textbook that has some exercises to help you trace recursive code: Read Me 1 
You have attempted of activities on this page.
opendsa-server.cs.vt.edu/OpenDSA/Books/Everything/html/Trace.html