Recursion

Author Topic: Recursion  (Read 14932 times)

Offline chinmay.sahooTopic starter

  • Trade Count: (0)
  • Full Member
  • ***
  • Thank You 2
  • Posts: 158
  • Karma: 0
    • We Helpline
Recursion
« on: 01-11-2017, 05:31:40 »
Recursion is an advanced topic, and you can certainly write JavaScript without using it. However in many situations, including some of the more
interesting examples later in this book, recursion is useful. A recursive function is usually one that calls itself. It is similar to a loop with or without a
terminating condition. Some programming functions lend themselves to recursive algorithms, such as the factorial one. The factorial is a classic
recursive algorithm because it is very obvious. A simple mathematical definition of the factorial operation is:

Code:
Only registered users can see contents. Please click here to Register or Login.
Factorial is commonly represented by the exclamation mark (!).

With this algorithm, let’s calculate 3!:

1. fact(3) = 3 * fact(2)
2. fact(2) = 2 * fact(1)
3. fact(1) = 1 * fact(0)
4. fact(0) = 1

As you can see, the algorithm works its way down from the given number to zero, so when it gets to fact(0) it must work its way back up to the
numbers.

We know that fact(0) = 1.
fact(1) = 1 * fact(0) = 1 * 1 = 1
fact(2) = 2 * fact(1) = 2 * 1 = 2
fact(3) = 3 * fact(2) = 3 * 2 = 6


Offline raghuramastrologer

  • Trade Count: (0)
  • Semi-Newbie
  • *
  • Thank You 0
  • Posts: 20
  • Karma: 0
  • Gender: Male
    • Astrologer Raghuram in Toronto
Re: Recursion
« Reply #1 on: 07-16-2019, 01:57:37 »
Recursion is the process which comes into existence when a function calls a copy of itself to work on a smaller problem. Any function which calls itself is called recursive function, and such function calls are called recursive calls.