Lecture 4

# Specifically if factorial calls itself then there are

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: elf. Specifically, if “factorial” calls itself, then there are two parameters “n” that have NOTHING to do with each other. 7 factorial(3): factorial(2): factorial(1): factorial(0): n = 3 calls factorial(2) n = 2 calls factorial(1) n = 1 calls factorial(0) returns 1 to factorial(1). Inside factorial(1), 1 * factorial(0) becomes: 1*1 factorial(1): returns 1 to factorial(2), Inside factorial(2), 2 * factorial(1) becomes: 2*1 8 factorial(2) returns 2 to factorial(3). Inside factorial(3), 3 * factorial(2) becomes: 3*2 A problem can be solved recursively if the following conditions are fulfilled: So, inside factorial(3), the return value is 3 * 2 = 6. This is what will be returned to the program. 1 - The problem can be decomposed into several problems such that (at least) one of them is of the “same kind” as the initial problem but “simpler” and all the others are “easy” cout << factorial...
View Full Document

## This document was uploaded on 03/31/2014 for the course CS 114 at NJIT.

Ask a homework question - tutors are online