Wednesday, January 29, 2014

Week of Jan.26.2013

This week we went over a little more recursion, and learned about inheritance in our class.  Luckily for me I am familiar with inheritance, but recursion was always a difficulty of mine to trace.  Recursion can be thought of as a piece wise function, where the base case is the case where the function stops calling itself, and begins to return the value from the previous function call until the function finally returns a final value.  One example can include a factorial function, where as the base case where if the input x is 0, the function returns 1, otherwise multiply the input with the function call input x-1.

Sudocode:

if x = 0 => 1
else => x*f(x-1)


def factorial(x):
    if x == 0:
        return 1
    else:
        return x*factorial(x-1)

     

No comments:

Post a Comment