Thursday, 5 February 2015

Tracing Recursion

Tracing recursion seems quite mind boggling really.

Think of it this way. There's a base case, where a simple input to a recursive function returns a simple result. This base case is there for two reasons: the first being to trick the tracer into thinking that recursive functions are easy to trace, and the other being that the way a recursive function works is by reducing complicated inputs to that base case.

So what happens if a more complex input is used? Well the recursive function takes that input, performs an operation on it to return a result, takes that result, and uses that as the next input for the recursive function.

Wait, what?

Yeah, it sounds complicated at first, but with a piece of paper and pencil in hand, it becomes rather fun to trace. You basically just keep calling the function until you get to, you guessed it, the base case, and then you pray that the function stops somewhere.

Trust me. It gets easier with practice.

No comments:

Post a Comment