Saturday 31 December 2011

Recursive Function

Definition....
  • A recursive function is one that calls itself repetitively until a final call is made thet no longer requires a self-call.
    • Functions that call themselves
    • Can only solve a base case
    • Divide a problem up into
      • What it can do
      • What it cannot do
        • What it cannot do resembles original problem
        • The function launches a new copy of itself (recursion step) to solve what it cannot do.
      • Eventually base case gets solved
        • Gets plugged in, works its way up and solves whole problem.


Recursion: Factorials of nonnegative numbers

  • Example: factorials
    • 5! = 5 * 4 * 3 * 2 * 1
    • Notice that 
      • 5! = 5 * 4!
      • 4! = 4 * 3!......
    • It can compute factorials recursively
    • Solve base case ( 1! = 0! = 1) then plug in
      • 2! = 2 * 1! = 2 * 1 = 2 ;
      • 3! = 3 * 2! = 3 * 2 = 6 ;

Recursive Evaluation of 5!








No comments:

Post a Comment