C++ gurus and learners, we program ourselves :P all work is CC licensed

Saturday, November 16

Recursive functions

19:30 By

Recursive functions

Recursive functions are way much difficult then simple functions
  1. in a function you place set of commands in a bracket and use it as many time as u want
  2. in recursive functions you make such a function which with the help of other functions and the repetition of it's own body gives you output ; complex but short code.
As compared to recursive function simple function when called runs specific commands inside it's body (could be loop but still simple as for command) it's decision criteria is small or let's say limited.
In recursive function you make some kind of super function which has wide choice of features including that it's body is small.
Definition: When a function calls itself or other functions while execution it is said to be recursive function.
Here is a simple example : 
To print a countdown form a digit that user inputs up to '0'
  1. #include <iostream>
  2. using namespace std;
  3. void sample_recursive_function(int N)
  4. {
  5.            if (N==0) { cout<<"N"<<endl; }
  6.          else  { cout<<"N"<<endl; --N; sample_recursive_function(N);}
  7. }
  8. int main()
  9. {
  10.           int N;
  11.           cout<<"Enter the number: ";
  12.           cin>>N;
  13.           sample_recursive_function(N);
  14.           return 0;
  15. }
Enter the number: 5                                                                                                                                                       
5                                                                                                                                                                                              
4                                                      -                                                                                                                                      
3                                                                                                                                                                                              
2                                                                                                                                                                                              
1                                                                                                                                                ^   --                                     
0                                                                                                                                                                       -                     
Press any key to continue . . . .                                                                                                                                  

In this function there is not much as coding or might say complex coding but it is a complex logic. This is what is difference between these functions.
This function can also call other functions described in this program hence you can generate loop in which more then 1 functions will work with each other to generate desired output;

             A -------------> B
                                      |
              |                             |
              |                             |
              |                            V
            D <-------------  C
In this diagram functions are related to each other because when you call one function in another function the control goes to this function and once this function is finished then it will allow for control to go back to main called function..
Here is an example :
  1.              int sum(int a , int b )                                      //f1
  2.             {            
  3.                         int c=  a+b;
  4.                         if  ( check_type(c)  == 1 )
  5.                        { return c;
  6.                         else
  7.                                 c+=1;
  8.             }

  9.             int check_type(int x)                                   //f2
  10.             {
  11.                       int check=x; 
  12.                      if ( x%2==0 ) 
  13.                       { return 1;} 
  14.                      if ( c%2!=0 )
  15.                        { return 0; }
  16.             }
Here f1 is calling f2 to change it's output. A simple recursive function.
Hope you understood the concept of recursive functions; gud luck; Allah HAfiz

0 comments :

Post a Comment