Home     Blog

Recursion

Recursion is an ability of a program to call itself, either directly or indirectly. Its a programming technique that naturally implements the divide and conquers programming approach. It does the task of calling itself. To do this, it must reduce its size upon every call; else, the process will become infinite.

Old recursion clip image002 Recursion

Type of Recursion

  • Direct Recursion
  • Indirect Recursion

Old recursion clip image004 Recursion

Example: To compute factorial of a number

Old recursion clip image006 Recursion

The above program works as follows,

fact(5) = {if(5= =1) is false thus return 5* fact (4)}

fact(4) = {if(4= =1) is false thus return 4* fact (3)}

fact(3) = {if(3= =1) is false thus return 3* fact (2)}

fact(2) = {if(2= =1) is false thus return 2* fact (1)}

fact(1) = {if(1= =1) is true thus return 1}

Old recursion clip image008 Recursion

Example: The Fibonacci series

Old recursion clip image010 Recursion

Old recursion clip image012 Recursion

VN:F [1.9.17_1161]
Rating: 0.0/10 (0 votes cast)
Follow Daniel Memetic on

Comments (1)

 

  1. kamal says:

    good illustratio but i also kno about tree and tail recursion

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)

Leave a Reply

Related Posts

  • Binary Search

    This method works on sorted lists by progressively making better guesses to find the location of a search key. Illustration Consider the following array: 3 10 15 20 35 40 60 Suppose we want to search the element "15" We take beg = 0, end = 6 and compute the location of the middle element [...]...


  • goto Statement

    In programming, significance has always been given to use of structured programming technique to build reliable software that are easy to debug, maintain and modify. In some cases, performance is more important than strict obedience to structured programming technique. In these cases, some unstructured programming technique may be used. For example, we can use break [...]...


  • Stacks

    Stacks are one of the commonly used data structures. A stack is also known as a last in first out (LIFO) system. It can be considered as a linear list in which insertion and deletion can take place only at one end called the top. This structure operates in much the same way as a [...]...


  • Functions

    A function is a named unit of a group of program statements. This unit can be invoked from other parts of the program. A programmer can solve a simple problem in C++ with a single function. Difficult and complex problems can be decomposed into sub-problems, each of which can be either coded directly or further [...]...


  • Linear Queue

    Implementation of operations on a linear queue: Creating an Empty Queue Before we can use a queue, it must be created. The purpose of initializing the queue is served by assigning -1 (as a sentinel value) to the front and rear variables. Note that the valid range of an index for the array is 0 [...]...