Thursday, 3 March 2016

Recursion


    This week I learned about recursion. Recursion is a technique that allows a function to call itself and can be a pretty powerful and useful tool.  For example, you can use recursion when finding the factorial of a number, as shown in the following:

Fact(n)
if n==1:
    return 1
else:
    return n x Fact(n-1)

     In this example, the Fact function is calling itself and calculating the factorial of n by breaking down the task to two steps.
     Recursion takes a task and breaks it into smaller pieces that are more manageable. I find this technique very interesting and am excited to do more practice on recursive functions. With such a powerful tool I think that it is important to use such a technique wisely. Currently I am still wondering which cases recursion should be used and when it should be dismissed. The reason I ask is because recursion is very elegant but can sometimes be inefficient. For instance, recursive sequences store lots of data and take time. I believe that as a computer scientist it is crucial for one to understand when such a powerful tool is needed and when it is unnecessary. It is important to develop these skills so that you can write a simple yet efficient program. Hopefully I can have these questions answered when I ask my T.A and professor this week.

No comments:

Post a Comment