Monday, July 25, 2011

Python Recursion

#!/usr/bin/python

def factorial(n):
    space = '*' * (4 * n)
    print space, 'factorial', n
    if n == 0:
        print space, 'returning 1'
        return 1
    else:
        recurse = factorial(n-1)
        result = n * recurse
        print space, 'returning', result
        return result

x=factorial(3)
print x

----
Draw a flow chart for this program
What if we change x=factorial(3) to x=factorial(5)
Change the flow chart

No comments:

Post a Comment