1 / 5

CS105 Lab 11 – Recursion

CS105 Lab 11 – Recursion. Announcements Midterm 2 may have been harder/longer than we had anticipated We will grade Midterm 2 completely before making any further announcements please watch this space!. Objective: Understanding Recursion.

naida
Download Presentation

CS105 Lab 11 – Recursion

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS105 Lab 11 – Recursion • Announcements • Midterm 2 may have been harder/longer than we had anticipated • We will grade Midterm 2 completely before making any further announcements • please watch this space! CS 105 – Fall 2009

  2. Objective: Understanding Recursion • A recursive function can call itself one or more times • with “smaller” arguments • General structure: Private Function recFunction(data As … ) As … If <base case condition> Then recFunction = <base case answer> Else recFunction = recFunction(smaller data) End If End Function Recursive function call CS 105 – Fall 2009

  3. Mystery Function 1 • Open lab11.xls and study the code for Mystery 1 • Predict what the answer will be for: • mystery1(5) • mystery1(6) • mystery1(7) • Now try it and see if you are correct! CS 105 – Fall 2009

  4. Mystery Function 2 • Mystery function 2 is more interesting: Private Function mystery2(strX As String) As Boolean • The data is of type String and the answer is of type Boolean (True/False) • We get “smaller” strings using the Mid function • Mid(string, startPosition, numberOfLetters) • Predict the answer for these cases, then check it: • mystery2(“A”) • mystery2(“Radar”) • mystery2(“radar”) CS 105 – Fall 2009

  5. Mystery Function 3 • Mystery function 3 is more complex: Function mystery3(intN, intM As Integer) As Integer • The first If condition is not a base case! • It is a recursive case • Predict the answer for these cases, then check it: • mystery3(5, 6) • mystery3(6, 5) • mystery3(2, 6) CS 105 – Fall 2009

More Related