Week 7 – Recursion
A
function in python is a recursive function if it calls itself again within the
body of the current function. Recursion is a powerful tool as it was seen in
Assignment 1. We had to create a recursive function for Tower of Hanoi with
four stools. It saved a great deal of time compared to loops and other methods.
One of the most important things in a recursive function is to understand what
you are exactly trying to accomplish. If you are trying to calculate the sum of
the numbers in nested lists, you would use recursive to get the sum. This makes
another issue arise. How do you know when to stop the recursion? You need a well-defined
stopping state so it will not lead into an infinite loop. Another thing to note
is that there is a maximum number of recursive steps Python can do before it
runs out of memory and crashes. You can use recursion for many purposes, such
as; factorials, sum of nested numbers and Fibonacci numbers. Recursion is a
tool you really need to understand when learning programming, it will help you
a great deal and save a lot of time when programming more complex systems. If
you question when to or when not to use recursion, just thing about this: Do I have
to do the same thing more than once?
Because
Week 7 occurred on a reading week, this blog entry may be a little shorter than
the other blog entries. We had no lectures or tutorials this week. We have our
first midterm on Wednesday. Reading week gave me the time to let me prepare for
the midterm and I feel confident going to write the test. I reviewed all the
material and I feel like I need to learn a little bit more about recursive
structures to feel fully confident.
Assignment
2 part 1 was handed out a few days ago. I took a quick glimpse at it but I have
not begun one bit yet due to midterms. I will put all my time into it after the
midterm.
Glad to see that your understanding of recursion has been improved over the last few weeks!
ReplyDelete