Flashcards
Recursion
Cards in this deck (7)
- 1What is recursion?When a subroutine (often a function) can call itself within its own subroutine. This means that each recursion will create a new copy of the function and remember all of the values that each function contains
- 2How does recursion work?Every time a recursive function calls itself, the process has to remember where it was in the program before it jumps to the new call, so it knows where to return to afterwards. This means that each recursion creates a new copy of the function but still remembers the previous versions of the function. It also has to remember the values of all of the variables because they are local to the function. This is done using stacks.
- 3What is the difference between recursion and iteration?Iteration is when the same block of code is repeated until a specified condition has been met.
Recursion is when the same subroutine is called (copied) repeated until a specified condition has been met. - 4What must a recursive function contain?- a stopping condition
- it must call itself
- a stopping condition that is reachable after a countable amount of time - 5what are the benefits of recursion over iteration?- more natural to read
- quicker to write/ less lines of code as some functions are naturally recursive
- suited to certain problems
- can reduce the size of a problem with each call. - 6what are the drawbacks of recursion rather than iteration- can run out due to too many calls causing it to crash
- more difficult to trace/follow as each frame on the stack has its own set of variables
- requires more memory than the equivalent iterative algorithm
- usually slower than iterative methods. - 7why is recursion more memory intensive?- each recursive call stores the current state on the stack and creates a new variable. It has to remember the state of all the variables for all of the called functions
- iteration uses the same variables
Information
- Description
- A level Computer Science (Unit 2 - Topic 2) Flashcards on Recursion, created by Ariana Parr on 20/02/2022.
- Created by
- Ariana Parr
- Views
- 0
- Rating