examples/book/digital-artists/chapter5/Listing5.16.ck
raw| 1 | // Listing 5.16 Computing factorial by using recursion |
| 2 | |
| 3 | fun int factorial( int x) |
| 4 | { |
| 5 | if ( x <= 1 ) |
| 6 | { |
| 7 | // when we reach here, function ends |
| 8 | return 1; |
| 9 | } |
| 10 | else |
| 11 | { |
| 12 | // recursive function calls itself |
| 13 | return (x*factorial(x-1)); |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | // Main Program, call factorial |
| 18 | <<< factorial(4) >>>; |
| 19 |