{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

SFTW120-LectureNotes-18

# SFTW120-LectureNotes-18 - SFTW120 Programming Science...

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

1 SFTW120 Programming Science Lecture Notes 18: C: Recursion © Robert P. Biuk-Aghai SFTW120 - Programming Science 2 Lecture Notes 18 Recursion square6 A function in C can make use of recursion: the function repeats an action by calling itself square6 Example: int factorial(int n) { if (n == 0) return(1); else return(n * factorial(n - 1)); } square6 Function factorial is called from within function factorial : this is a recursive call

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

View Full Document
2 SFTW120 - Programming Science 3 Lecture Notes 18 Using recursion square6 Recursion is suitable for problems where: box2 One or more simple cases of the problem exist that have an easy, non-recursive solution box2 The other cases can be redefined in terms of problems that are closer to the simple cases box2 Redefining the problem repeatedly with each function call, the problem can be reduced to many simple cases which are easy to solve square6 Pseudocode: if this is a simple case solve it else redefine the problem using recursion SFTW120 - Programming Science 4 Lecture Notes 18 Recursion in factorial
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 7

SFTW120-LectureNotes-18 - SFTW120 Programming Science...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online