Sei sulla pagina 1di 7

Recursive Function Definition

Recursive function is a function that contains a call to itself. C supports creating recursive function with ease and efficient.

Why Recursive Function


Recursive function allows you to divide your complex problem into identical single simple cases which can handle easily. This is also a well-known computer programming technique: divide and conquer.

Note of Using Recursive Function


Recursive function must have at least one exit condition that can be satisfied. Otherwise, the recursive function will call itself repeatly until the runtime stack overflows.

Recursive Processes:
Most programmers with a few years under their belt understand recursive procedures. A recursive procedure is simply a procedure that calls itself. Understanding this is somewhat of a "rite of passage" for many programmers. In this post we will take the discussion slightly further from recursive procedures and discuss recursive processes. Computer Science is not so much the study of procedures as the study of processes.

Implementation
| | | | | int f1 (int n) { if (n == 0) return 1; return n * f1 (n - 1); }

Process

Recursionin C
Recursive function is closely related to definitions of functions in mathematics so we can solving factorial problems using recursive function. All you know in mathematics the factorial of a positive integer N is defined as follows: N! = N*(N-1)*(N-2)2*1; Or in a recursive way: N! = 1 if N <=1 and N*(N-1)! if N > 1 The C recursive function to calculate factorial:
01 # include<stdio.h> 02 03 int factorial(unsigned int number) 04 { 05 06 if(number <= 1) return 1;

07 return number * factorial(number - 1); 08 } 09 void main() 10 { 11 12 13 } int x = 5; printf("factorial of %d is %d",x,factorial(x));

The output of program

factorial of 5 is 120

In theory, all recursive functions can be rewritten by using iteration. In case number of recursive calls exceeded, the runtime stack can be overflow, you have to implement your recursive function using iteration. for example we can rewrite the factorial calculation using iteration as follows:
1 int factorial(unsigned int number){ 2 int f = 1; 3 4 while(number < 0) f *= number--;

5 return f 6}

Features :

There should be at least one if statement used to terminate recursion. It does not contain any looping statements.

Advantages :

It is easy to use. It represents compact programming structures.

Disadvantages :

It is slower than that of looping statements because each time function is called.

Efficiency of Recursion Recursive methods often have slower execution times when compared to their iterative counterparts The overhead for loop repetition is smaller than the overhead for a method call and return If it is easier to conceptualize an algorithm using recursion, then you should code it as a recursive method The reduction in efficiency does not outweigh the advantage of readable code that is easy to debug

Simulating recursion
1. Towers of Hanoi using recursion:

The Tower of Hanoi or Towers of Hanoi, also called the Tower of Brahma or Towers of Brahma, is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another rod, obeying the following rules:

Only one disk may be moved at a time. Each move consists of taking the upper disk from one of the rods and sliding it onto another rod, on top of the other disks that may already be present on that rod. No disk may be placed on top of a smaller disk.

A key to solving this puzzle is to recognize that it can be solved by breaking the problem down into a collection of smaller problems and further breaking those problems down into even smaller problems until a solution is reached. The following procedure demonstrates this approach.

label the pegs A, B, Cthese labels may move at different steps let n be the total number of discs number the discs from 1 (smallest, topmost) to n (largest, bottommost)

To move n discs from peg A to peg C: 1. move n1 discs from A to B. This leaves disc n alone on peg A 2. move disc n from A to C 3. move n1 discs from B to C so they sit on disc n The above is a recursive algorithm: to carry out steps 1 and 3, apply the same algorithm again for n1. The entire procedure is a finite number of steps, since at some point the algorithm will be required for n = 1. This step, moving a single disc from peg A to peg B, is trivial. This approach can be given a rigorous mathematical formalism with the theory of dynamic programming
Program: #include<stdio.h> #include<conio.h> #include<math.h> void <strong class="highlight">hanoi</strong>(int x, char from,char to,char aux) { if(x==1) { printf("Move Disk From %c to %c\n",from,to); } else { <strong class="highlight">hanoi</strong>(x-1,from,aux,to); printf("Move Disk From %c to %c\n",from,to); <strong class="highlight">hanoi</strong>(x-1,aux,to,from); } } void main() { int disk; int moves; clrscr(); printf("Enter the number <strong class="highlight">of</strong> disks you want to play with:"); scanf("%d",&disk); moves=pow(2,disk)-1; printf("\nThe No <strong class="highlight">of</strong> moves required is=%d \n",moves); <strong class="highlight">hanoi</strong>(disk,'A','<strong class="highlight">C</strong>','B');

getch(); }

2. Fibonacci Series:
By definition in mathematics, the Fibonacci Numbers are the numbers in the below sequence:

By definition, the first Fibonacci number is 0 and second Fibonacci number is 1. Then each subsequent number is calculated based on sum of the previous two. Follow on this definition, the Fibonacci number Nth can be calculated as the formular follows:

with seed values

In Mathematics, this is also called recurrence relation.

Recursive Fibonacci Series C function


1 int Fibonacci(int n) 2{ 3 4 5 6 7 8 9} return Fibonacci(n-1) + Fibonacci(n-2); if ( n == 0 ) return 0; if ( n == 1 ) return 1;

Fibonacci Series C function using Iterative Method


01 int Fibonacci(int n) 02 { 03 04 int f1 = 0; int f2 = 1;

05 06 07 08 09 10 11 12 13 }

int fn; for ( int i = 2; i < n; i++ ) { fn = f1 + f2; f1 = f2; f2 = fn; }

Potrebbero piacerti anche