Lesson Objectives Aims 1 To be able to
Lesson Objectives Aims 1. To be able to comprehend and explain the process of recursion (2. 2. 1(b)) 2. To be able to give examples of where recursion is or could be used Key Words
Recursion • This is recursion
Recursion • Recursion is when a procedure calls itself. • Simple as that. What’s this recursion all about? Alan, I’m fired.
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!
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
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.
Recursion • Exam question check! • (click for mark scheme answer)
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!
Recursion • Now we’ve done binary search for five year olds, let’s see how recursion can help…
Recursion • Spot the recursion! 1 sub count (By. Val n as integer) 2 if n <= 10 then 3 count(n+1) 4 else 5 return 0 6 end if 7 console. writeline(n) 8 end sub • 1. Work out what this subroutine does and on what line recursion occurs. • 2. Complete a trace table for the programme.
Recursion 1 sub count (By. Val n as integer) 2 if n <= 10 then 3 count(n+1) 4 else 5 return 0 6 end if 7 console. writeline(n) 8 end sub • Trace Table Line n (1 st call) 1 5 2 3 6 n (2 nd call) n (3 rd call) n (4 th call)
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)
Recursion
Recursion
Recursion
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 oughta know
- Slides: 16