cs32w11dis4sol - CS32 Introduction to Computer Science II...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Discussion 2C Notes (Week 5, February 4) TA: Brian Choi ([email protected] ) Section Webpage: Recursion A recursion is a function-writing technique where the function refers to itself . Assuming you have learned the concept of recursion sufficiently in the lecture, we’ll focus on the tricks in writing a recursive function. The Way to Think About Recursion Writing a recursive function involves the division of a problem into subproblems. A good example is the factorial function that computes n! = n x (n-1) x (n-2) x ... x 2 x 1 for some arbitrary n 0 . Here is the iterative way of writing factorial : Iterative functions are more intuitive than recursive functions because we can see the whole process -- you see how factorial goes from 1 to n and multiply them up in that for loop. When writing the recursive version of the same function, you need not worry about “going from 1 to n .” Instead, we only worry about “going from n-1 to n ,” believing factorial(n-1) will return the right value . In factorial ’s case, if you know the value of factorial(n-1) , it is very easy to compute factorial(n) -- we only need to multiply n to the value of factorial(n-1) (because n * factorial(n-1) = n * {(n-1)*(n-2)*...*2*1} = factorial(n) ). This is what’s meant by “going from n-1 to n .” Translating this into a C++ code, we get the following function: My factorial function on the left simply assumes that factorial(n-1) returns what we want, and computes the value of factorial(n) based on this assumption. When factorial(n-1) is called, then it makes the same assumption and uses the value of factorial(n-2) to compute its value, ... , and so on. For example, if factorial(5) is called, it relies on the value factorial(4) returns. When factorial(4) is called, it relies on the value factorial(3) returns. When factorial(3) is called, it relies on the value factorial(2) ... Wait, there is something wrong here. We seem to be stuck in an infinite recursion. We need to stop this loop at some point.
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern