121 Week 9 - Recursion Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Iteration and recursion

A

Used to traverse a set of values or run the same logic a given number of times.
Any recursive solution can be written in an iterative way.
Any iterative solution can be written in a recursive way.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Iteration

A

A function which uses loops to execute the same logic multiple times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Recursion

A

A function which calls itself to execute the same logic multiple times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Order of execution in recursion.

A

The order in which logic is executed in recursion depends on if the logic is before or after the function calls itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Single recursion

A

A recursive function where you only need to have one self-call for each function call.
It is generally easy to write iteratively.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Multiple recursion

A

A recursive function where you need to have more than one self-call for each function call.
It is generally harder to write iteratively because we need to explicitly track state.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Reasons to use iteration

A

Memory usage is controlled explicitly by the programmer, so a “stack overflow“ less likely.
Can executes more quickly, as there is no overhead from stack frame creation / destruction.
Iterative functions can be easier to understand than a recursive function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Reasons to use recursion

A

Recursive functions are much more concise than iterative functions.
Languages which support tail recursion can eliminate some of the extra cost to performance, and stack overflows.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly