Update
This commit is contained in:
parent
5c43f56dc3
commit
1c2717e515
8 changed files with 48 additions and 29 deletions
21
recursion.md
21
recursion.md
|
@ -18,7 +18,26 @@ unsigned int factorial(unsigned int x)
|
|||
}
|
||||
```
|
||||
|
||||
See that as long as x > 1, recursive calls are being made; with each the x is decremented so that inevitably x will at one point come to equal 1. Then the *else* branch of the condition will be taken -- the terminating condition has been met -- and in this branch no further recursive call is made, i.e. the recursion is stopped here and the code starts to descend from the recursion.
|
||||
See that as long as x > 1, recursive calls are being made; with each the x is decremented so that inevitably x will at one point come to equal 1. Then the *else* branch of the condition will be taken -- the terminating condition has been met -- and in this branch no further recursive call is made, i.e. the recursion is stopped here and the code starts to descend from the recursion. The following diagram show graphically computation of `factorial(4)`:
|
||||
|
||||
```
|
||||
factorial(4) = 4 * 6 = 24
|
||||
| ^
|
||||
| | return
|
||||
| '------------.
|
||||
| call |
|
||||
'-----> factorial(3) = 3 * 2 = 6
|
||||
| ^
|
||||
| | return
|
||||
| '------------.
|
||||
| call |
|
||||
'-----> factorial(2) = 2 * 1 = 2
|
||||
| ^
|
||||
| | return
|
||||
| '-----.
|
||||
| call |
|
||||
'------> factorial(1) = 1 <-- end condition met
|
||||
```
|
||||
|
||||
Note that even in computing we can use an infinite recursion sometimes. For example in [Hashell](haskell.md) it is possible to define infinite [data structures](data_structure.md) with a recursive definition; however this kind of recursion is intentionally allowed, it is treated as a mathematical definition and with correct use it won't crash the program.
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue