Monday, 7 October 2013

Recursion is (Recursion is (Recursion is (Recursion isn't quite the simplest case) great) great) great


Recursion is a Computer Science tool used to solve problems that are made up of smaller instances of the original problem. In most object oriented programing languages functions are able to call themselves allowing the programer to run the same method inside itself, each time with potentially different values in the parameters. At some point the method should hit a simplest case in which it can no longer be divided into small versions of itself. This point must always happen or the method will run itself for as long as the IDE can handle without ever returning values (or null) to the above methods. A method can employ single recursion, in which the method only calls itself once and expands linearly. However for more complex problems multiple recursion may be necessary as function in turn calls multiple versions of itself, expanding exponentially.

For certain problems Recursion can be a very efficient alternative to repetitively writing very similar lines of code.

However it can also be challenging to follow the recursive method during run time making it difficult to debug.

2 comments:

  1. Can you think of examples for which a recursive function would be more efficient than an iterative function and vice versa?

    ReplyDelete
  2. A Recursive function could be more efficient when searching through a Binary Search Tree of undetermined size because it's simpler to look at the problem as smaller versions of the bigger problem. However if the tree was linear in nature and we were doing the same thing to it every time (such as add 1 to the item) it might be more efficient to use a for loop rather then repeatedly calling a new method for every item.

    ReplyDelete