Recursion and Iteration (OCR A-Level Computer Science): Flashcards
📚Flashcards
Practise the cards
13 cards from this deck
ShowHide
Practise the cards
13 cards from this deck
What is recursion?
What is recursion?
Function calls itself to solve smaller instances of same problem
Base case in recursion
Base case in recursion
Condition that stops the recursion
Recursive case
Recursive case
Part of function that calls itself with smaller/simpler input
Call stack role in recursion
Call stack role in recursion
Stores current state of each call; unwinds when base case reached
What is iteration?
What is iteration?
Uses loops (FOR/WHILE) to repeat code until condition is met
Which uses more memory: recursion or iteration?
Which uses more memory: recursion or iteration?
Recursion uses more memory due to call stack
Which is faster: recursion or iteration?
Which is faster: recursion or iteration?
Iteration is generally faster & more efficient
Stack overflow risk
Stack overflow risk
Can occur in recursion if base case is missing
Infinite loop risk
Infinite loop risk
Can occur in iteration if loop condition is incorrect
Key benefit of recursion
Key benefit of recursion
Simplifies code for complex problems; more elegant & clear
Key benefit of iteration
Key benefit of iteration
More efficient; uses less memory; avoids stack overflow
Why does recursion use more memory?
Why does recursion use more memory?
Each recursive call uses stack memory; can cause stack overflow
Trace tables purpose
Trace tables purpose
Track variable values at each step to debug & understand behaviour
