Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson Objectives Aims Key Words

Similar presentations


Presentation on theme: "Lesson Objectives Aims Key Words"— Presentation transcript:

1 Lesson Objectives Aims Key Words
To be able to comprehend and explain the process of recursion (2.2.1(b)) To be able to give examples of where recursion is or could be used Key Words

2 Recursion This is recursion

3 What’s this recursion all about?
Recursion is when a procedure calls itself. Simple as that. What’s this recursion all about? Alan, I’m fired.

4 Recursion It may be necessary to re-run the procedure with new parameters as part of a loop. We get a procedure calling itself for a while before returning back to the main body of the programme. This removes the need to the procedure to exit, hand parameters back to the main code only for the parameters to be sent straight back to it. You wouldn’t turn the TV off and on again every time you wanted to change channel!

5 Recursion There’s positive and negative points about recursion. It’s important to consider these when planning anything recursive. Can be easier to read and write recursive code Recursion often creates a more sensible/natural solution - no leaving/re-entering a procedure repeatedly in one go. Some tasks are just naturally recursive Recursion requires more memory Recursion is difficult to trace as each call has its own set of variables It is possible to run out of memory if too many calls occur

6 Recursion When you look for examples of recursion you will always be given factorisation – I will not be talking about this because it melts my brain. The truth is that there are many more, practical and easy to understand uses for recursion than hell-maths.

7 Recursion Exam question check! (click for mark scheme answer)

8 Take this binary search as an example…
Recursion Take this binary search as an example… You should all know how a binary search works – remind me…. Find the median of a list and split the list here Does the median match the target? Is the median greater than the target? If it is, it will take the half of the list to the right and repeat the process until the target is found. If not, it’ll go to the left and repeat the process still!

9 Recursion Now we’ve done binary search for five year olds, let’s see how recursion can help…

10 2. Complete a trace table for the programme.
Recursion Spot the recursion! 1. Work out what this subroutine does and on what line recursion occurs. 2. Complete a trace table for the programme. 1 2 3 4 5 6 7 8 sub count (ByVal n as integer) if n <= 10 then count(n+1) else return 0 end if console.writeline(n) end sub

11 Recursion Trace Table 1 2 3 4 5 6 7 8 sub count (ByVal n as integer)
if n <= 10 then count(n+1) else return 0 end if console.writeline(n) end sub Line n (1st call) n (2nd call) n (3rd call) n (4th call) 1 5 2 3 6

12 Make a flow chart for the algorithm that we just traced.
Recursion It’s pretty easy to show recursion in flow chart form as well. The subroutine will just call itself the way you’d expect it to be called from the main programme. Make a flow chart for the algorithm that we just traced. cheese (wensleydale, 14)

13 Recursion

14 Recursion

15 Recursion

16 Review/Success Criteria
You should know What recursion is! When it’s sensible to use recursion and what problems that can arise when using it How to spot a recursive algorithm How to follow a recursive algorithm You you you oughta know


Download ppt "Lesson Objectives Aims Key Words"

Similar presentations


Ads by Google