50 likes | 158 Views
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.
E N D
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
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
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
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
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