click below
click below
Normal Size Small Size show me how
Recursion
| Question | Answer |
|---|---|
| What is recursion? | Recursion is where a function calls itself |
| How is recursion different from a loop? Which is better for performance? | It can accomplish the same thing as a loop, but it makes the solution clearer There is no performance benefit to using recursion (loops may in fact be better), but it can make the program easier to read. |
| What are the 2 parts of a recursive function? | Recursive functions consist of a base case and recursive case. Recursive case is when the function calls itself. Base case is when the function stops calling itself so it doesn’t go into an infinite loop. |
| What is a stack? | A simple data structure w/ a predefined capacity. It allows adding and removing objects in a particular order. Items can only be added to the top of the stack, and the only element that can be removed is the element at the top of the stack |
| What's a good analogy for a stack? | A pile of post-it notes. You can only add notes to the top of the stack, and the last one added must be the first removed. |
| What are the 2 operations for a stack? | push and pop |
| Describe the call stack | When a function is called, a certain amount of memory is set aside for that function to use for purposes such as storing local variables. It is also used by the computer to store info about the function, like the function's address in memory. |
| Why do recursive functions have the tendency to use a lot of memory? | Keeping track of the information about active functions and their variables can use up a lot of memory if the recursion goes many levels deep. If you have a lot of function calls, your program will run significantly slower. |