I’ve long had mixed feelings about recursion. (I’ve written about recursion several times in this blog.) In one post, Recursion Early, Recursion Late, I wrote about the suggestion that recursion be dropped from a first programming or computer science course. In another post I asked if it should be added to a list of Four Key Concepts of Computer Programming. Well recently over dinner a colleague stated that students learn recursion better if they learn it before they learn loops. A second colleague responded that they had seen the same thing. Apparently they both feel that students have to unlearn something if they learn loops before recursion. I have to wonder if that is the reason I struggled with recursion as a student.
Honestly I am not a fan of using recursion for looping. Recursion feels like a resource intensive way to do things that don’t really require it. On the other hand educators I know who start with functional languages such as Scheme tell me that recursion is very natural and easy to learn/use if taught first. This is probably true – that recursion is more natural in functional languages. I remember a time when “does the language support recursion” was a reasonable question that was often answered in the negative. So there was a time when many of us didn’t see recursion as all that practical. I can’t imagine a language today getting far without support for recursion.
Which brings me back to where I started. Recursion has a lot of similarity with “normal” loops. One needs to set the initial state(s), do some sort of work, and setup an properly terminating condition and test. Recursive routines without a clean and clear end state to start the unrolling create huge memory problems and stack overflows. Poorly constructed loops generally either terminate early or never terminate at all. Either way you get an interesting debugging problem. So given all that perhaps it does make sense to start with recursion.
One final note, the colleague who started this conversation told us that she had taught this course twice in a row. The first group of students really took to recursion and tended to use it when other sorts of loops would be as easy or easier to use. The second group complained asking why they had to learn the hard way (recursion) first when loops were so much easier. So you never know how students are going to respond.
So do you teach recursive loops first or some other types of loops first? What works for you?