Recursion for GCE AS Computing

695 views

Published on

This is a simple introduction to coding simple recursion using the mathematical Factorial algorithm.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
695
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
10
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Recursion for GCE AS Computing

  1. 1. Recursion<br />Simple explanations<br />
  2. 2. Recursive routine<br />It calls itself<br />Repetitive problems simplified<br />Take care to have an exit case<br />
  3. 3. An example<br />Factorial in mathematics<br />The result of successively multiplying an integer in reductions of 1<br />Indicated by an exclamation mark (!)<br />Example 5! = 5 * 4 * 3 * 2 * 1 = 120<br />The function is very useful in statistics<br />
  4. 4. Rewriting the Factorial<br />5! = 5 * 4 * 3 * 2 * 1 = 120<br />We can rewrite the section <br />4 * 3 * 2 * 1 as 4!<br />It’s Factorial 4<br />
  5. 5. Rewriting the Factorial<br />These are all equivalent<br />5! = 5 * 4 * 3 * 2 * 1 (120)<br />= 5 * 4! (24)<br />= 5 * 4 * 3! (6)<br />= 5 * 4 * 3 * 2! (2)<br />= 5 * 4 * 3 * 2 * 1! (1)<br />= 5 * 4 * 3 * 2 * 1 * 0! (1)<br />
  6. 6. Expressing in code<br />Using pseudo code (modelled on Visual Basic)<br />Create a function to return the outcome:<br />Function Factorial (n as integer) as Integer<br />Function identifier = Factorial.<br />That’s the name that will be called<br />Parametern is the input parameter value<br />The ‘as Integer’ defines the type to be returned<br />
  7. 7. Executing the code<br />The code runs normally until:<br />It meets the call to itself<br />The code pauses and a new call is started<br />The parameter passed acts as <br />Stopping Condition<br />
  8. 8. Executing continued<br />When stopping condition reached<br />The value is calculated<br />Control is passed back to the previous call<br />This is done successively back to the original call<br />
  9. 9. Pseudo Code (1)<br />Function Factorial (n as Integer) as Integer<br />Return n * Factorial (n-1)<br />End Function<br />If 5 was the value passed it would be<br />Return 5 * Factorial (4)<br />
  10. 10. Using the stopping condition<br />01 Function Factorial (n as Integer) as integer<br />02 If n = 1 then <br />03 Return 1<br />04 Else<br />05 Return n * Factorial (n-1)<br />06End if<br />07 End function<br />RECURSIVE CALL IS LINE 05<br />
  11. 11. Essential nature<br />This is another form of iteration loop<br />It is sometimes thought as ’Elegant’<br />The next slide compares iteration and recursion<br />
  12. 12. Factorial as iterative WHILE loop<br />Function Factorial (n as Integer) as integer<br /> Dim intTemp as integer<br />intTemp = 1<br />WHILE n > 1<br /> intTemp = intTemp * n<br /> n = n -1<br /> LOOP<br /> RETURN intTemp<br />End function<br />
  13. 13. Compare Iteration/Recursion<br />
  14. 14. The essential bits<br />It calls itself<br />Needs a stopping condition<br />When next call met pause and start new call<br />Progressive until stopping condition<br />Control passed back ‘up-the-chain’<br />Every recursion can be written as an iteration loop<br />Remember the comparisons for exam<br />

×