lec12-recursion

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

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

1 CSC 1111 Introduction to Computing using C++ Recursion

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

View Full Document
2 Outlines Solving problems recursively How to implement and trace recursive functions Examples
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

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

View Full Document
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?
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.

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

View Full Document
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)
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

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

View Full Document
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) ; }
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern