lec12-recursion - CSC 1111 Introduction to Computing using...

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

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ Recursion
Image of page 1

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

View Full Document Right Arrow Icon
2 Outlines Solving problems recursively How to implement and trace recursive functions Examples
Image of page 2
3 Introduction Recursion – a method of defining functions in which the function being defined may be used within its own definition. e.g.: f(N) = N * (N-1) * (N-2) * … * 1 f(N) = N * f(N-1), if N > 1 f(1) = 1 Recursive solution
Image of page 3

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

View Full Document Right Arrow Icon
4 int f(int N) { if (N == 1) return 1; return N * f(N-1) ; } f(4) N = 4 Recursive solution implemented as a C++ function. How should you trace this recursive function?
Image of page 4
5 int f(int N) { if (N == 1) return 1; return N * f(N-1) ; } int f(int N) { if (N == 1) return 1; return N * f(N-1) ; } N = 4 N = 3 f(3) The function is not actually "calling itself" but a "clone" of itself. Each "clone" of the function maintains its own set of local variables.
Image of page 5

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

View Full Document Right Arrow Icon
6 int f(int N) { if (N == 1) return 1; return N * f(N-1) ; } int f(int N) { if (N == 1) return 1; return N * f(N-1) ; } int f(int N) { if (N == 1) return 1; return N * f(N-1) ; } N = 4 N = 3 N = 2 f(2)
Image of page 6
7 int f(int N) { if (N == 1) return 1; return N * f(N-1) ; } int f(int N) { if (N == 1) return 1; return N * f(N-1) ; } int f(int N) { if (N == 1) return 1; return N * f(N-1) ; } int f(int N) { if (N == 1) return 1; return N * f(N-1); } f(1) N = 4 N = 3 N = 2 N = 1
Image of page 7

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

View Full Document Right Arrow Icon
8 int f(int N) { if (N == 1) return 1; return N * f(N-1) ; } int f(int N) { if (N == 1) return 1; return N * f(N-1) ; }
Image of page 8
Image of page 9
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