Recursion
Recursion
symbol table stack frames
Recursion
Recursive function
"function that calls itself"
start 5 recursive_function(5) end 5
start 4 recursive_function(4) end 4
start 3 recursive_function(3) end 3
start 2 start 1
recursive_function(2) end 2 recursive_function(1) end 1
recursive_function(0) done
Python shell
> def recursive_function(x): if x > 0: print("start", x) recursive_function(x - 1) print("end", x) else: print("done")
> recursive_function(5)
| start 5 | start 4 | start 3 | start 2 | start 1 | done | end 1 | end 2 | end 3 | end 4 | end 5
Recursion
x
0
stack frame
x
1
x
2
x
3
x
4
x
5
recursive_function global variables
Recursions stack when x = 0 is reached
Python shell
> def recursive_function(x): if x > 0: print("start", x) recursive_function(x - 1) print("end", x) else: print("done")
> recursive_function(5)
| start 5 | start 4 | start 3 | start 2 | start 1 | done | end 1 | end 2 | end 3 | end 4 | end 5
Python shell
> def rec(x): if x > 0: print("start", x) rec(x - 1) rec(x - 1) print("end", x) else: print("done")
Recursion tree
rec(3)
rec(2)
rec(2)
rec(1)
rec(1)
rec(1)
rec(1)
rec(0) rec(0) rec(0) rec(0) rec(0) rec(0) rec(0) rec(0)
Python shell
> rec(3)
| start 3 | start 2 | start 1 | done | done | end 1 | start 1 | done | done | end 1 | end 2 | start 2 | start 1 | done | done | end 1 | start 1 | done | done | end 1 | end 2 | end 3
Question ? How many times does rec(5) print "done"?
Python shell
> def rec(x): if x > 0: print("start", x) rec(x - 1) rec(x - 1) rec(x ? 1) print("end", x) else: print("done")
a) 3 b) 5 c) 15 d) 81 e) 125 f) 243 = 35 g) Don't know
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- recursion and linked lists 3
- jest test coverage report html
- recursion and exceptions
- kirchhoff depth migration using maximum amplitude
- freenas bug 6720
- qgis application bug report 18877
- uct algorithm circle intermediate class recursion part 2
- lab 16 recursion
- problems with recursion tail recursion
- tail recursion
Related searches
- c recursion reverse string
- python recursion examples
- c recursion example
- reverse string recursion java
- python recursion limit
- recursion in python
- simple recursion examples
- recursion in c programming
- max recursion depth python
- python increase recursion depth
- when to use recursion programming
- python set recursion depth