A look back to my programs pt 2: recursion

To learn more about Python, I was reading the book How to Think Like a Computer Scientist: Learning with Python by Allen Downey, Jeffrey Elkner, and Chris Meyers -which you can get from here– and in chapter 4.9  I read about recursion.

Basically, recursion is when you create a function that calls itself. This solves some kind of problems that would otherwise require the use of a loop. In the book there was an example of this in chapter 5.5, in which a factorial function was defined using recursion. As perhaps you may remember (or not), I had already done a program that calculated the factorial of a number, and I showed it on my post called Factorial Calculator!

So, after I learned about this topic I decided to redo the program, but using recursion to see how it worked.

I did it like this:

factorial_corrected

Notice I also used .upper() to get the result to the question of whether the user wished to do it again or not. I talked about this method on my last post.

Anyway, the program ran exactly like the first one I made:

factorial_correct-run

Here is a useful tutorial to understand this better:

 

5 thoughts on “A look back to my programs pt 2: recursion

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s