SlideShare a Scribd company logo
Recursion101 Simple explanations for AS F452
Recursive routine It calls itself. Repetitive problems simplified. Must have an exit case to avoid endless loop.
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
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
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
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)
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 Parametern is the input value (suppliedas Integer) The ‘as Integer’ defines the type to be returned
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
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
Pseudo Code (1) Function Factorial (n as Integer) as Integer Return n * Factorial (n-1) End Function 	n is the variable holding the values If 5 was the value passed it would be Return 5 * Factorial (4)
Pseudo code (2) 01 Function Factorial (n as Integer) as Integer 02 	If n = 1 then  03 	Return 1 04	Else 05	Return n * Factorial (n-1) 06End if 07 End function RECURSIVE CALL IS LINE 05
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)
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)
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!
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
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
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
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
Factorial as iterative WHILE loop Function Factorial (n as Integer) as integer 	Dim intTemp as integer intTemp = 1 WHILE n > 1 		intTemp = intTemp * n 	n = n -1 	LOOP 	RETURN intTemp End function
Compare Iteration/Recursion
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

More Related Content

What's hot

Functions and tasks in verilog
Functions and tasks in verilogFunctions and tasks in verilog
Functions and tasks in verilog
Nallapati Anindra
 
Logarithmic function, equation and inequality
Logarithmic function, equation and inequalityLogarithmic function, equation and inequality
Logarithmic function, equation and inequality
Felina Victoria
 
CSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow ChartsCSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow Charts
Yhal Htet Aung
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
GOWSIKRAJAP
 
11 doloops
11 doloops11 doloops
11 doloops
fyjordan9
 
Fuzzy control design_tutorial
Fuzzy control design_tutorialFuzzy control design_tutorial
Fuzzy control design_tutorial
Resul Çöteli
 
Reduction & Handle Pruning
Reduction & Handle PruningReduction & Handle Pruning
Reduction & Handle Pruning
MdAshikJiddney
 
Unit 3
Unit 3 Unit 3
Unit 3
GOWSIKRAJAP
 
2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problem2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problem
Frankie Jones
 
Merge Sort
Merge SortMerge Sort
Merge Sort
Nikhil Sonkamble
 
Module 4 topic 1 part 3 notes
Module 4 topic 1 part 3 notesModule 4 topic 1 part 3 notes
Module 4 topic 1 part 3 notes
Annie cox
 
curve fitting or regression analysis-1.pptx
curve fitting or regression analysis-1.pptxcurve fitting or regression analysis-1.pptx
curve fitting or regression analysis-1.pptx
abelmeketa
 
Functions part1
Functions part1Functions part1
Functions part1
yndaravind
 
Solving Simultaneous Linear Equations-1.pptx
Solving Simultaneous Linear Equations-1.pptxSolving Simultaneous Linear Equations-1.pptx
Solving Simultaneous Linear Equations-1.pptx
abelmeketa
 
Solving of Non-Linear Equations-1.pptx
Solving of Non-Linear Equations-1.pptxSolving of Non-Linear Equations-1.pptx
Solving of Non-Linear Equations-1.pptx
abelmeketa
 
Algorithms and flowcharts ppt (seminar presentation)..
 Algorithms and flowcharts  ppt (seminar presentation).. Algorithms and flowcharts  ppt (seminar presentation)..
Algorithms and flowcharts ppt (seminar presentation)..
Nagendra N
 
Flowchart and algorithm
Flowchart and algorithmFlowchart and algorithm
Flowchart and algorithm
DHANIK VIKRANT
 
4 1 functions
4 1 functions4 1 functions
4 1 functions
hisema01
 
Integration of function on non standard form
Integration of function on non standard formIntegration of function on non standard form
Integration of function on non standard form
Lily Maryati
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
Rohit Shrivastava
 

What's hot (20)

Functions and tasks in verilog
Functions and tasks in verilogFunctions and tasks in verilog
Functions and tasks in verilog
 
Logarithmic function, equation and inequality
Logarithmic function, equation and inequalityLogarithmic function, equation and inequality
Logarithmic function, equation and inequality
 
CSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow ChartsCSC1100 - Chapter12 - Flow Charts
CSC1100 - Chapter12 - Flow Charts
 
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING18CSS101J PROGRAMMING FOR PROBLEM SOLVING
18CSS101J PROGRAMMING FOR PROBLEM SOLVING
 
11 doloops
11 doloops11 doloops
11 doloops
 
Fuzzy control design_tutorial
Fuzzy control design_tutorialFuzzy control design_tutorial
Fuzzy control design_tutorial
 
Reduction & Handle Pruning
Reduction & Handle PruningReduction & Handle Pruning
Reduction & Handle Pruning
 
Unit 3
Unit 3 Unit 3
Unit 3
 
2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problem2.3 Apply the different types of algorithm to solve problem
2.3 Apply the different types of algorithm to solve problem
 
Merge Sort
Merge SortMerge Sort
Merge Sort
 
Module 4 topic 1 part 3 notes
Module 4 topic 1 part 3 notesModule 4 topic 1 part 3 notes
Module 4 topic 1 part 3 notes
 
curve fitting or regression analysis-1.pptx
curve fitting or regression analysis-1.pptxcurve fitting or regression analysis-1.pptx
curve fitting or regression analysis-1.pptx
 
Functions part1
Functions part1Functions part1
Functions part1
 
Solving Simultaneous Linear Equations-1.pptx
Solving Simultaneous Linear Equations-1.pptxSolving Simultaneous Linear Equations-1.pptx
Solving Simultaneous Linear Equations-1.pptx
 
Solving of Non-Linear Equations-1.pptx
Solving of Non-Linear Equations-1.pptxSolving of Non-Linear Equations-1.pptx
Solving of Non-Linear Equations-1.pptx
 
Algorithms and flowcharts ppt (seminar presentation)..
 Algorithms and flowcharts  ppt (seminar presentation).. Algorithms and flowcharts  ppt (seminar presentation)..
Algorithms and flowcharts ppt (seminar presentation)..
 
Flowchart and algorithm
Flowchart and algorithmFlowchart and algorithm
Flowchart and algorithm
 
4 1 functions
4 1 functions4 1 functions
4 1 functions
 
Integration of function on non standard form
Integration of function on non standard formIntegration of function on non standard form
Integration of function on non standard form
 
3 algorithm-and-flowchart
3 algorithm-and-flowchart3 algorithm-and-flowchart
3 algorithm-and-flowchart
 

Similar to Recursion for GCE AS Computing

Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
Flavia Tembo Kambale
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
Ziyauddin Shaik
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
josies1
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptx
ajajkhan16
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
Muhammad Hammad Waseem
 
WT-Pravesh Sakhare.pptx
WT-Pravesh Sakhare.pptxWT-Pravesh Sakhare.pptx
WT-Pravesh Sakhare.pptx
TuleshwarGupta1
 
Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptx
PochupouOwo
 
2022-9-recursive-1.pptx
2022-9-recursive-1.pptx2022-9-recursive-1.pptx
2022-9-recursive-1.pptx
RayLee547290
 
Recursion examples
Recursion examplesRecursion examples
Recursion examples
HafsaZahran
 
Iterations and Recursions
Iterations and RecursionsIterations and Recursions
Iterations and Recursions
Abdul Rahman Sherzad
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Exercise 1 Write a C program that contains the following s.pdf
Exercise 1 Write a C program that contains the following s.pdfExercise 1 Write a C program that contains the following s.pdf
Exercise 1 Write a C program that contains the following s.pdf
mail354931
 
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
jacksnathalie
 
MP 4 – Continuation-Passing StyleCS 421 – Fall 2012Revis.docx
MP 4 – Continuation-Passing StyleCS 421 – Fall 2012Revis.docxMP 4 – Continuation-Passing StyleCS 421 – Fall 2012Revis.docx
MP 4 – Continuation-Passing StyleCS 421 – Fall 2012Revis.docx
rosemarybdodson23141
 
lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptx
Lizhen Shi
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
NUST Stuff
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questions
Gradeup
 
Ma3696 lecture 4
Ma3696 lecture 4Ma3696 lecture 4
Ma3696 lecture 4
Brunel University
 
Introducing to Asynchronous Programming
Introducing to Asynchronous  ProgrammingIntroducing to Asynchronous  Programming
Introducing to Asynchronous Programming
Александр Федоров
 

Similar to Recursion for GCE AS Computing (20)

Recursion.pdf
Recursion.pdfRecursion.pdf
Recursion.pdf
 
Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )Introduction to python programming ( part-2 )
Introduction to python programming ( part-2 )
 
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
Task4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docxTask4output.txt 2  5  9 13 15 10  1  0  3  7 11 14 1.docx
Task4output.txt 2 5 9 13 15 10 1 0 3 7 11 14 1.docx
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
Unit-I Recursion.pptx
Unit-I Recursion.pptxUnit-I Recursion.pptx
Unit-I Recursion.pptx
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
 
WT-Pravesh Sakhare.pptx
WT-Pravesh Sakhare.pptxWT-Pravesh Sakhare.pptx
WT-Pravesh Sakhare.pptx
 
Introduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptxIntroduction to Dynamic Programming.pptx
Introduction to Dynamic Programming.pptx
 
2022-9-recursive-1.pptx
2022-9-recursive-1.pptx2022-9-recursive-1.pptx
2022-9-recursive-1.pptx
 
Recursion examples
Recursion examplesRecursion examples
Recursion examples
 
Iterations and Recursions
Iterations and RecursionsIterations and Recursions
Iterations and Recursions
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
 
Exercise 1 Write a C program that contains the following s.pdf
Exercise 1 Write a C program that contains the following s.pdfExercise 1 Write a C program that contains the following s.pdf
Exercise 1 Write a C program that contains the following s.pdf
 
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docxE2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
E2 – Fundamentals, Functions & ArraysPlease refer to announcemen.docx
 
MP 4 – Continuation-Passing StyleCS 421 – Fall 2012Revis.docx
MP 4 – Continuation-Passing StyleCS 421 – Fall 2012Revis.docxMP 4 – Continuation-Passing StyleCS 421 – Fall 2012Revis.docx
MP 4 – Continuation-Passing StyleCS 421 – Fall 2012Revis.docx
 
lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptx
 
Lecture#6 functions in c++
Lecture#6 functions in c++Lecture#6 functions in c++
Lecture#6 functions in c++
 
Classical programming interview questions
Classical programming interview questionsClassical programming interview questions
Classical programming interview questions
 
Ma3696 lecture 4
Ma3696 lecture 4Ma3696 lecture 4
Ma3696 lecture 4
 
Introducing to Asynchronous Programming
Introducing to Asynchronous  ProgrammingIntroducing to Asynchronous  Programming
Introducing to Asynchronous Programming
 

Recently uploaded

BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
Nguyen Thanh Tu Collection
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
TechSoup
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
David Douglas School District
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
nitinpv4ai
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
RidwanHassanYusuf
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
IsmaelVazquez38
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapitolTechU
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
Nguyen Thanh Tu Collection
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
danielkiash986
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
Celine George
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
zuzanka
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
heathfieldcps1
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
Celine George
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
Prof. Dr. K. Adisesha
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
Iris Thiele Isip-Tan
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
TechSoup
 

Recently uploaded (20)

BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 9 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2024-2025 - ...
 
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
Elevate Your Nonprofit's Online Presence_ A Guide to Effective SEO Strategies...
 
Juneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School DistrictJuneteenth Freedom Day 2024 David Douglas School District
Juneteenth Freedom Day 2024 David Douglas School District
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 
Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)Oliver Asks for More by Charles Dickens (9)
Oliver Asks for More by Charles Dickens (9)
 
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptxBIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
BIOLOGY NATIONAL EXAMINATION COUNCIL (NECO) 2024 PRACTICAL MANUAL.pptx
 
Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.Bossa N’ Roll Records by Ismael Vazquez.
Bossa N’ Roll Records by Ismael Vazquez.
 
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptxCapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
CapTechTalks Webinar Slides June 2024 Donovan Wright.pptx
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
BÀI TẬP BỔ TRỢ TIẾNG ANH LỚP 8 - CẢ NĂM - FRIENDS PLUS - NĂM HỌC 2023-2024 (B...
 
Pharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brubPharmaceutics Pharmaceuticals best of brub
Pharmaceutics Pharmaceuticals best of brub
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17How to Download & Install Module From the Odoo App Store in Odoo 17
How to Download & Install Module From the Odoo App Store in Odoo 17
 
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptxRESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
RESULTS OF THE EVALUATION QUESTIONNAIRE.pptx
 
The basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptxThe basics of sentences session 7pptx.pptx
The basics of sentences session 7pptx.pptx
 
How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17How Barcodes Can Be Leveraged Within Odoo 17
How Barcodes Can Be Leveraged Within Odoo 17
 
Data Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsxData Structure using C by Dr. K Adisesha .ppsx
Data Structure using C by Dr. K Adisesha .ppsx
 
Educational Technology in the Health Sciences
Educational Technology in the Health SciencesEducational Technology in the Health Sciences
Educational Technology in the Health Sciences
 
Leveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit InnovationLeveraging Generative AI to Drive Nonprofit Innovation
Leveraging Generative AI to Drive Nonprofit Innovation
 

Recursion for GCE AS Computing

  • 2. Recursive routine It calls itself. Repetitive problems simplified. Must have an exit case to avoid endless loop.
  • 3. 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
  • 4. 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
  • 5. 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
  • 6. 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)
  • 7. 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 Parametern is the input value (suppliedas Integer) The ‘as Integer’ defines the type to be returned
  • 8. 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
  • 9. 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
  • 10. Pseudo Code (1) Function Factorial (n as Integer) as Integer Return n * Factorial (n-1) End Function n is the variable holding the values If 5 was the value passed it would be Return 5 * Factorial (4)
  • 11. Pseudo code (2) 01 Function Factorial (n as Integer) as Integer 02 If n = 1 then 03 Return 1 04 Else 05 Return n * Factorial (n-1) 06End if 07 End function RECURSIVE CALL IS LINE 05
  • 12. 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)
  • 13. 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)
  • 14. 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!
  • 15. 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
  • 16. 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
  • 17. 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
  • 18. 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
  • 19. Factorial as iterative WHILE loop Function Factorial (n as Integer) as integer Dim intTemp as integer intTemp = 1 WHILE n > 1 intTemp = intTemp * n n = n -1 LOOP RETURN intTemp End function
  • 21. 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