recursion for gce as computing

21
Recursi on 101 Simple explanations for AS F452

Upload: pgmckillop

Post on 01-Jul-2015

513 views

Category:

Education


0 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Recursion for GCE AS Computing

Recursion101

Simple explanations for AS F452

Page 2: Recursion for GCE AS Computing

Recursive routine

• It calls itself.• Repetitive problems simplified.• Must have an exit case to avoid endless loop.

Page 3: Recursion for GCE AS Computing

Non-Geek explanation

• A recursive problem could be represented by the dolls.

• The same problem is used by each but it depends on working it the answer from the next smaller.

• When you have the answer to the calculation of the smallest you can work out the rest by going back up through to the largest doll.

• Russian Dolls

Page 4: Recursion for GCE AS Computing

An example

• Factorial in mathematics– The result of successively multiplying an integer in

reductions of 1– Indicated by an exclamation mark (!)

• Example 5! = 5 * 4 * 3 * 2 * 1 = 120• The function is very useful in statistics– See an example here

Page 5: Recursion for GCE AS Computing

Rewriting the Factorial

• 5! = 5 * 4 * 3 * 2 * 1 = 120

• We can rewrite the section – 4 * 3 * 2 * 1 as 4!– It’s Factorial 4

Page 6: Recursion for GCE AS Computing

Rewriting the Factorial

• These are all equivalent• 5! = 5 * 4 * 3 * 2 * 1 (120)• 5 * 4! (24)• 5 * 4 * 3! (6)• 5 * 4 * 3 * 2! (2)• 5 * 4 * 3 * 2 * 1! (1)• 5 * 4 * 3 * 2 * 1 * 0! (1)

Page 7: Recursion for GCE AS Computing

Expressing in code

• Using pseudo code (modelled on Visual Basic)• Create a function to return the outcome:– Function Factorial (n as integer) as Integer• Function identifier = Factorial.

– That’s the name that will be called

• Parameter n is the input value (supplied as Integer)• The ‘as Integer’ defines the type to be returned

Page 8: Recursion for GCE AS Computing

Executing the code

The code runs normally until:– It meets the call to itself– The code pauses and a new call is started– The parameter passed acts as • Stopping Condition when it reaches a set value

Page 9: Recursion for GCE AS Computing

Executing continued

• When stopping condition reached• The value is calculated• Control is passed back to the previous call• This is done successively back to the original

calls, all stored on The Stack

Page 10: Recursion for GCE AS Computing

Pseudo Code (1)

Function Factorial (n as Integer) as IntegerReturn n * Factorial (n-1)

End Function

n is the variable holding the values

If 5 was the value passed it would beReturn 5 * Factorial (4)

Page 11: Recursion for GCE AS Computing

Pseudo code (2)

01 Function Factorial (n as Integer) as Integer02 If n = 1 then 03 Return 104 Else05 Return n * Factorial (n-1)06 End if07 End functionRECURSIVE CALL IS LINE 05

Page 12: Recursion for GCE AS Computing

Step-by-step Example

• We trace the calculation of 3! -> Factorial 3– We’ll follow the execution of the code– Concentrating on the recursive call (yellow highlight)

Page 13: Recursion for GCE AS Computing

Execution (1)

• Imagine your program analyses sets of data• At some stage in your code:– you need to calculate 3! (Factorial 3)– This is 3 * 2 * 1 and evaluates to 6

• You want to assign it to variable intAnswer• The function is called by:

intAnswer = Factorial(3)intAnswer is assigned the outcome of Factorial(3)

Page 14: Recursion for GCE AS Computing

Execution (2) of Factorial(3)

• n = 3• Line 02 is FALSE• Jumps to Line 05• Return means ‘calculate’

• Has to calculate:• 3 * Factorial(3-1)• = 3 * Factorial(2)• That’s the recursive call!

Page 15: Recursion for GCE AS Computing

Execution (3) of Factorial(3)

• We got to 3 * Factorial(2)• To work out Factorial(2)• Returns to Line 02• n = 2 -> Line 02 = False• Jump to Line 05• Return 2 * Factorial(2-1)• = 2 * Factorial(1)• Another recursive call

Page 16: Recursion for GCE AS Computing

Execution (4) of Factorial(3)

• We got to 2 * Factorial(1)• To work out Factorial(1)• Returns to Line 02• n = 2 -> Line 02 = True• Function returns value = 1• STOPPING CONDITION

REACHED• It can now go back through

the stack

Page 17: Recursion for GCE AS Computing

Execution (5) of Factorial(3)

• Working back through the stack• Factorial(1) = 1• Factorial(2) -> 2 * Factorial(1) -> 2*1 = 2• Factorial(3) -> 3 * Factorial(2) -> 3 * 2 = 6• Factorial(3) execution is complete• Variable assignment in the code can complete• intAnswer = Factorial(3) = 6

Page 18: Recursion for GCE AS Computing

Essential nature

• This is another form of iteration loop• It is sometimes thought as ’Elegant’• The next slide compares iteration and

recursion– This is where the higher marks lie• Moving past straight facts and evaluating

Page 19: Recursion for GCE AS Computing

Factorial as iterative WHILE loop

Function Factorial (n as Integer) as integerDim intTemp as integerintTemp = 1WHILE n > 1

intTemp = intTemp * nn = n -1

LOOPRETURN intTemp

End function

Page 20: Recursion for GCE AS Computing

Compare Iteration/RecursionIteration Recursion

Uses a loopEnd condition controls loopNeeds instructions to do loop

Repeats itself until stopping conditionThe successive simplification must end eventually

Only one function callOne set of variables – less memory used

Many function calls Each has own variables – more memory used

Variables need to trackVariables should be initialised

Variables automatically createdVariables automatically initialised

The code may be more difficult to understand

Code usually simpler to understand and closer to the way a human would explain the algorithm

Page 21: Recursion for GCE AS Computing

The essential bits

• It calls itself• Calls depend on the one

below• Needs a stopping

condition• When next call met

pause and start new call• Progressive until

stopping condition

• Control passed back ‘up-the-chain’

• Every recursion can be written as an iteration loop

• Remember the comparisons for exam

• You should be able to identify recursive use