Thursday, December 20, 2012

Recursive Function: Factorial: Part 1

Factorial


Recursive function is a function that partially calls itself. It is often used for combinatorial search and sorting methods. A simple good example that uses recursive program is factorial, which is mathematically denoted as “n!”

1! = 1
2! = 2 x 1 = 2
3! = 3 x 2 x 1 = 6
4! = 4 x 3 x 2 x 1 = 24
5! = 5 x 4 x 3 x 2 x 1 = 120
6! = 6 x 5 x 4 x 3 x 2 x 1 = 720


 



 
In the above example, the condition, “if n = 0”, is called a base case. When the base case is true, or when the recursion reaches this point, the function finishes and exits. 

 


No comments:

Post a Comment