Recursion Flashcards

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

what is recursion

A

when a function calls itself
this produces repetition

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

when is recursion used

A

to make code repeat
as an alternative to iteration

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

what are the advantages and limitations of recursion

A

+ shorter and simpler code

  • each recursion uses up space in memory
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

structure of a recursive function

A
  • header w/ name of func and parameters
  • base case - an if statement, ends as the final return
  • the name of the function itself with any other parameters
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

the base case

A

an if statement

typically at the top of a recursive function defenition

if the test is true , the recursion stops

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

how are parameters used in recursuon

A

pass values along the chain of functions that open in memory

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

how to convert a recursive function to iteration

A
  • turn the if line of the base case into the header of a while loop, reversing the logic
  • indent the commands inside the while loop
  • delete recursive lines
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how to convert an iterative function to recursion

A
  • convert the header of the while loop into a base case (if statement) , reversing the logic

add a recursive line at the end of the program

consider whether you need to use parameters

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

similarity between recursion and iteration

A

used to repeat blocks of code

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

differences between recursion and iteration

A

recursive function often solves a problem in fewer lines of code, but uses more memory

iterative function overrwrites same area of memory repeatedly, using less space

iterative functions use more lines to solve the problem

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