recursion

=3.2.2 g=
 * ===identify and use recursion to solve problems; show an understanding of the structure of a recursive subroutine, including the necessity of a stopping condition;===
 * == KEYWORDS == || == DEFINITIONS (from the BCS Dictionary) == ||
 * Recursion || When a function or procedure will call itself. It will usually perform tests on arguments before running. ||
 * Subroutine || A set of instructions that performs a specific task but is not a complete program. ||
 * Stopping condition || A recursive function will not stop by itself so will need some means of exiting. ||

If n is a positive integer, then the factorial of that integer can be calculated: n! = n x (n-1)!

In programming, this could be written as: factorial(n) = n * factorial(n-1)

It would be possible to write this out as a function on the computer. If you were to do it as per the function above, it would look like this: 1 Function factorial(ByVal number As Integer) 2 Return number * factorial(number - 1) 3 End Function

However, this will not work. This is because there is **nothing to tell the computer** **when to stop** and this is where the use of **recursion** comes into play. As you know that **1! = 1**, as soon as the computer has a value of 1, then the result can be worked out as 1. So the corrected version of the above code is: 1 Function factoria(ByVal number As Integer) 2 If number = 1 Then 3 Return = 1 4 Else 5 Return number * factorial(number - 1) 6 End If 7 End Function

The final code will look like this: 1 Do 2 num = InputBox("Enter an integer number", "Input Data") 3 Loop Until n >= 0

4 If num = 0 Or num = 1 Then 5 fact = 1 6 Else 7 fact = 1 8 count = num 9 Do 10 fact = fact * count 11 count = count - 1 <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;">12 Loop Until count = 0 <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;">13 End If <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;">14 MsgBox("Factorial " & num & " is " & fact,, "Result")

Lines 1 - 3 make sure that a **number** has been **entered** that is **greater than zero**. Lines 4 - 5 will **check** if the entered number is **0 or 1**. If this is true, then the fact = 1 If this is not the case, then lines 7 - 12 are executed. Here, **fact** and **count variables** are **initialised** and a **loop** will begin, working out the factorial. Supposing the input of this program was five. What would the output be? <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;">1 Do <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;">2 num = InputBox("Enter an integer number", "Input Data") **The number is 5** <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;"> 3 Loop Until n >= 0 <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;"> 4 If num = 0 Or num = 1 Then **This is false so we skip to line 7** <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;">5 fact = 1 <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;">6 Else <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;">7 fact = 1 <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;">8 count = num **num = 5 therefore count = 5** <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;"> 9 Do <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;"> 10 fact = fact * count **fact = 1 * 5 = 5** <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;">11 count = count - 1 **count = 5-1 and this continues until count = 0** <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;"> 12 Loop Until count = 0 <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;"> 13 End If <span style="color: #0000ff; font-family: "Lucida Console",Monaco,monospace;"> 14 MsgBox("Factorial " & num & " is " & fact,, "Result") **Factorial 5 is 120**

5(c) (i) State what is meant by recursion (ii) In which line of the above algorithm does recursion happen? || 2 (1 + 1) || 4(b) What is meant by a recursive function? || 3 || (ii) In a high-level language of your choice, write a recursive function to find the nth number in the sequence. Use the code below to help. || 9 (2 + 7) || = = =3.2.2 h=
 * == EXAM PAPER == |||| == QUESTION == || == MARKS == ||
 * Jan 2009 |||| [[image:Jan_2009_5(c)(i).jpg]]
 * Jun 2010 |||| [[image:Jun_2010_4(b)_-_4(c).jpg]]
 * Jun 2011 |||| 4(c) (i) Describe what is meant by a recursive algorithm?
 * ===trace the execution of a recursive subroutine including calls to itself;===


 * == KEYWORDS == || == DEFINITIONS (from the BCS Dictionary) == ||
 * Above definitions tie in with this section. ||

Have a look at the following algorithms for the function and the recursive function called Find(which is called by the main function):

1 Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

2 Dim num As Integer 3 Dim answer As Integer

4 num = InputBox("Enter a positive integer", "Input")

5 answer = find(1, num, num)

6 MsgBox("The answer is " & answer,, "Result")

7 End Sub

8 Function find(ByVal first As Integer, ByVal last As Integer, ByVal number As Integer)

9 Dim mid As Integer 10 Dim value As Integer 11 mid = Int((first + last) / 2) 12 If (mid * mid <= number) And (number < (mid + 1) * (mid + 1)) Then 13 value = mid 14 Else 15 If mid * mid > number Then 16 value = find(first, mid - 1, number) 17 Else 18 value = find(mid + 1, last, number) 19 End If 20 Return Value

21 End Function Whilst this may look very complicated, it does show how a recursive subroutine can call itself. An important note to make is that each time the recursive function is called, new storage will have to be found for first, last, number, mid and value. This is down to the fact that as the function calls itself it will have new values for the variables. So what if the input number was 7?

Important sections of code would look like this:

btnCalculate answer = find(1, 7, 7)

This will now take you to:

find( first = 1, last = 7, number = 7) mid = Int((1+7) / 2) = 4 If (4 * 4 <= 7) And (7 < (4+1) * (4 + 1)) Then (taking you to the next step below) value = mid Else (returns to the first step) If 4 * 4 > number Then value = find (1, 4-1, 7)

This will now lead on to:

find( 1, 3, 7) Mid = Int((1+3) DIV 2) = 2 If (2 * 2 <= 7) And (7 < (3) * (3)) Then value = 2 (returns back to the step above)

An important thing to notice here is that the **function** will **always return** to the **instruction after** the one that **has been called** by it.

5(d) State the result of the function if it called with the following arguments: (i) "AA" (ii) "BABAA" || 2 (1 + 1) || - Each time the function is called - The value of the argument in each call - Lines of algorithm executed - The value returned from each call || 6 || 4(c) What value will be returned by Mystery(5) and why? || 3 ||
 * == EXAM PAPER == |||| == QUESTION == || == MARKS == ||
 * Jan 2009 |||| [[image:Jan_2009_5(c)(i).jpg]]
 * Jan 2009 |||| 5(e) The function is called with "AA". Trace the execution of the algorithm showing:
 * Jun 2010 |||| [[image:Jun_2010_4(b)_-_4(c).jpg]]
 * Jun 2010 |||| 4(d) Trace the execution call of Mystery(15) showing all calls and the value returned. || 5 ||