Sunday, 2 March 2014

Recursion

Recursion is a nifty little programming trick in which a function calls itself. Depending on the nature of the function, recursion can be used to streamline a variety of tasks, such as parsing and creating nested objects, creating several objects containing values relative to each other in some way, or splitting a large algorithm into several small, easily calculable  parts. It's an important element of writing efficient code, as the same set of instructions are read every time a function calls itself again. However, it's also sometimes difficult to trace and keep track of all the interacting elements, as with each iteration there is a new set of input values and a different return value. But hey, no one said it was easy being beautiful.

I found recursion in python very difficult at first, making mistakes like forgetting to put return statements where they were needed (Note to any student readers: never forget the return statements.), returning data as the wrong type, calling the function with the incorrect number of variables, ect. Staying away from the single-line, ultra-dense return statement functions helped with tracing the code, seeing it spaced out and organized in an order that made sense to me helped a lot in labs, and by the first term test I felt comfortable enough to answer the recursion question in a single line.
At least I think I did.
The test was first thing in the morning and I was really sick, so the whole thing's kinda a blur.
Pretty sure I did well, though.

No comments:

Post a Comment