Recursion is a method of solving problems that involves breaking a problem down into smaller and smaller sub problems until you get to a small enough problem that it can be solved trivially. Usually recursion involves a function calling itself. While it may not seem like much on the surface, recursion allows us to write elegant solutions to problems that may otherwise be very difficult to program. It is legal for one function to call another, and you have seen several examples of that. It is also legal for a function to call itself. It may not be obvious why that is a good thing, but it turns out to be one of the most magical and interesting things a program can do. For example, look at the following function: def countdown(n): if n == 0: print "Blastoff!" else: print n countdown(n-1) A call to this function countdown(5) will print 5 4 3 2 1 Blastoff! Advantages of recursion Recursive functions make the code look clean and e...
Python for ktu students-by Dr Binu V P-9847390760. This Blog is written using python 2.x. Visit the new blog pythonformlktuminor.blogpost.com for python 3.x