Recursion is a programming tool which provides a powerful yet simple way of approaching numerous problems. Recursive functions can be summarized as a process being performed where one of the step is to "repeat the process". This makes it similar to a loop because of its ability to repeat the same lines of a code.

In programming context, if a piece of code allows you to call a function inside that same function, then it can be labelled as a recursive call of the function. Recursion makes it simpler to express ideas where the result of the recursive call is important to complete a task.

Let us take a simple example of recursive programming involving computing factorials. Factorial of a non-negative integer n denoted as n! can be computed as the product of all positive integers less than or equal to n.

For example,

factorial (5) is the same as 5*4*3*2*1, and factorial (3) is 3*2*1.

A point to be noted is that the factorial of a number is equal to the product of the same number and the factorial of the number immediately below it.

For example,

factorial (5) is the same as 5 * factorial (4).

The Recursive function in the part of code requires a condition to make it understand when to stop. Since factorials of numbers less than 1 does not make any sense, we stop at the number 1 and return the factorial of 1 (which is 1). Therefore, the factorial function will be as follows:

int factorial(int k) { if(k == 1) { return 1; } else { return k * factorial(k - 1); } }

The stopping point as discussed previously is called the base case. A base case is the bottom point of a recursive function where the operation is able to return an answer directly without calling the recursive function again. It is mandatory for all the recursive programs to have at least one base case. Otherwise the condition of infinite loop i.e. the program would run forever or until it ran out of memory or stack space.

Iteration and Recursion are both key Computer Science concepts that are used in creating numerous algorithms and developing software products.

*Read more about Iteration and Recursion here*

Let us go through the following sequence of steps to achieve recursive programming.

- Often a seed or base initial value is required in order to start with Recursive programming. This can be achieved using a parameter passed to the function. Also, we can accomplish this by providing a gateway function that is non-recursive in nature. This sets up a seed value for the recursive function calculation.
- Ensure, whether the current value or values being calculated or processed matches the base case. In the cases where it matches the base case, process and return the value.
- Smaller or simpler sub-problems can be used to represent the answers in a simpler and easy way.
- Execute or perform the algorithm on the sub-problems designed in the previous step.
- Formulate the answer by combining the results.
- Return the results.

PROGRAM:#include< iostream> using namespace std; int sumofnum(int k); int main() { int k; cout << "Enter a natural number:"; cin >> k; cout << "Sum of numbers = " << sumofnum(n); return 0; } Int sumofnum(int k) { if(k != 0) return k + sumofnum(k - 1); return 0; } OUTPUT: Enter a natural number: 10 Sum of numbers = 55

PROGRAM:#include < iostream> using namespace std; int cal_hcf(int k1, int k2); int main() { int k1, k2; cout << "Enter the value of two natural numbers: "; cin >> k1 >> k2; cout << "H.C.F of the entered numbers " << k1 << " & " << k2 << " is: " << cal_hcf(k1, k2); return 0; } int cal_hcf(int k1, int k2) { if (k2 != 0) return cal_hcf(k2, k1 % k2); else return k1; } OUTPUT: Enter the value of two natural numbers: 366 60 H.C.F of the entered numbers 366 and 60 is: 6

PROGRAM:class Cal_factorial { int factorial(int k) { int answer; if ( k ==1) return 1; answer = factorial (k-1) * k; return answer; } } class FactRecursion { public static void main (String args[]) { Cal_factorial f =new Cal_factorial(); System.out.println(“Using recursion Factorial of 3 is “ + f.factorial(3)); System.out.println(“Using recursion Factorial of 4 is “ + f.factorial(4)); System.out.println(“Using recursion Factorial of 5 is “ + f.factorial(5)); } } OUTPUT: Factorial of 3 is 6 Factorial of 4 is 24 Factorial of 5 is 120

PROGRAM: public class Recursion { static int k1=0,k2=1,k3=0; static void Fibo(int ct){ if(ct>0){ k3 = k1 + k2; k1 = k2; k2 = k3; System.out.print(" "+k3); Fibo(ct-1); } } public static void main(String[] args) { int ct=15; System.out.print(k1+" "+k2); Fibo(ct-2); } } OUTPUT: 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

- Recursion Reduce unnecessary calling of function.
- Recursion makes program elegant and cleaner.
- Through Recursion one can Solve problems in easy way while its iterative solution is very big and complex.
- All algorithms can be defined recursively which makes it easier to visualize and prove.
- If the speed of the program is vital then, you should avoid using recursion.

- It is very difficult to debug and understand the Recursive solution.
- In Recursive function the availability of an if statement is necessary somewhere to make the function to return back without executing recursive call, otherwise the function will never return.
- Recursion is not usually considerable when the program is small and running on a commodity hardware and takes a lot of stack space,
- More processor time is involved in Recursion.
- Recursions are generally slower and uses more memory. Instead, a loop can be used.

- Mitosis is a process in which a cell divides itself into half to make two identical copies. Mitosis takes place in all types of cells in the human body except few. More importantly, the human body builds itself using recursion algorithm.
- Recursion is used in one of those lifts with shiny mirror like inside wall. The view is recursive. There is a wall, within a wall and it continues. If you have a mirror in front of another mirror, it is a recursive view. However, this is a recursion without a terminating condition.
- Fractals is an abstract mathematical concept, but nature often surprisingly employs fractal geometry in the form of naturally occurring frost crystals.
- While browsing, following a bunch of links and then hitting the 'back' button makes us follow another series of links are all example of recursion.

- The famous Tower of Hanoi game comes under the context of Recursion. The objective of the puzzle is to move the entire stack to another rod. The key solution to this problem is to realize that the problem can be broken into smaller modules until a solution is reached.
- Recursion can be used to solve computer science problems such as Fibonacci series, factorial, sum of numbers, calculation of highest common factor and so.
- A linked list is a recursive data structure. It is either empty or consists of a node followed by a linked list which demonstrates the use of recursion.
- It is possible to solve Sudoku puzzles in reasonable amounts of time, using a simple recursion algorithm.
- Sorting algorithms in data structures can also be implemented using recursion algorithm.

Basic Subject

Computer Science

- Programming Assignment Help
- Database Help
- Data Structure Assignment Help
- Operating Systems Assignment Help
- Computer Network Assignment Help
- UML Diagram Assignment Help
- IT Assignment Help
- Game Programming
- Computer Science Assignment Help
- Medical Science Assignment Help
- Social Science Assignment Help
- Information Systems

Engineering

- Biochemical and Biotechnology Help
- Chemical Engineering Assignment
- Statistics Assignment Help
- Civil Engineering Assignment Help
- Electrical, Electronics Help
- Mathematics, Computing Assignment Help
- Mechanical and Industrial Engg. Help
- Petroleum Engg. Assignment Help
- Biochemistry Assignment Help
- Cell Biology Assignment Help
- Arts and Architecture Help
- Silverlight Assignment Help