Thursday, November 29, 2012

Inline Function, Macros and Recursion.



Inline function is the optimization  technique used by the compilers.  Inline functions are specified using the same syntax as any other function except that they include the inline keyword in the function declaration. While invoking an inline method does not generate a function call, but rather expands the body at the point of call. Use inline definitions only for methods that are short.  Use of too many inline function may cause thrashing in the memory. It also save overhead of variables on the stack.

The inline functions are similar to macros but inline functions are parsed by the compiler, whereas macros are expanded by the preprocessor.




Example of Macros : Largest of two no.




#include <iostream.h>

#define max(a,b) (a>b?a:b)  //Macro defined
  void main()
      {
           int x,y;
           cout<<"\n\tEnter two no ";
           cin>>x>>y;
           cout<<"\n\tLarges of - "<<x<<" and " <<y<<" is "<<max(x,y);
      }





Example of Inline Fuction : Largest of two no.




#include <iostream.h>

  inline int max(int x,int y)
      {
           return x>y?x:y;
      }
  void main()
      {
           int x,y;
           cout<<"\n\tEnter two no ";
           cin>>x>>y;
           cout<<"\n\tLarges of - "<<x<<" and " <<y<<" is "<<max(x,y);
      }



When function called itself to perform an finite iteration its called recursion. The Recursive functions call themselves to work towards a solution to a problem. While a recursive function perform, it is keep creating a new stack frame on top of the current, a stack is a special area of memory.  Whenever a method is called, an item is placed on the stack for each local variable that we passing to the method.

Tail recursive is method has the recursive call as the last statement in the method and the recursive methods that are not tail recursive are known as non-tail recursive. Tail Recursive functions are very easy to convert to an iterative one. Recursive functions are occupy more space and slow than normal iterative process but sometime when we need multiple loop use of recursion is very handy.



Example of Non-tail Recursive function : Bubble sort to call two recursive function for two loops.



//Bubble Sort

#include <iostream.h>

class rec_sort
{
 private: int Arr[100],n;
 public:
  rec_sort()
      {
           n = 0;
      }
   void accept()
      {
           int i;
           cout<<"\n\tEnter the value of n:";
           cin>>n;
           cout<<"\n\tEnter "<<n<<" Values \n";
           for(i=0;i<n;i++)
              {
                   cin>>Arr[i];
              }
      }
   void disp()
      {
           int i;
           for(i=0;i<n;i++)
              {
                   cout<<Arr[i]<<"  ";
              }
      }
  int sort(int i)
      {
           if(i>=n)
              {
                   return 0;
              }
           else
              {
                   chks(i,i+1);
                   sort(i+1);
              }
      }

  int chks(int i,int j)
      {
           int tmp;
              if(j>=n)
                   {
                        return 0;
                   }
              else
                {
                        if(Arr[i]>Arr[j])
                            {
                                tmp=Arr[i];
                                Arr[i]=Arr[j];
                                Arr[j]=tmp;
                            }
                        chks(i,j+1);
                }
      }
 };

  void main()
      {
          rec_sort RS;
          int tmp;
          RS.accept();
          cout<<"\nOriginal No : ";
          RS.disp();
          cout<<"\nSorted No   : ";
          tmp=RS.sort(0);
          RS.disp();
      }





Example of Tail Recursion – Sum of ‘N’ natural number.




#include <iostream.h>

//Sum of first N natural No.
  int sum(int n)
      {
           if(n<=1)
              {
                   return n;
              }
           else
              {
                   return n+sum(n-1);
              }
      }
  void main()
      {
           int s=0,n;
           cout<<"\n\tEnter the value of n  ";
           cin>>n;
           s=sum(n);
           cout<<"\n\tSum = "<<s;
      }




Example of Tail Recursion – GCD of two No.




#include <iostream.h>
  //GCD of two No.

  int gcd(int,int);
  inline int max(int x,int y)
      {
           return x>y?x:y;
      }

  inline int min(int x,int y)
      {
           return x<y?x:y;
      }

  void main()
      {
           int m,n,hcf;
           cout<<"\n\tEnter two No. ";
           cin>>m>>n;

           hcf=gcd(max(m,n),min(m,n));
           cout<<"\n\tGCD of "<<m<<" and "<<n<<" = "<<hcf;
      }
  int gcd(int m,int n)
      {

           if(n<=0)
              {
                   return m;
              }
           else
              {
                   return gcd(n,m%n);
              }
      }




No comments: