Wednesday, January 4, 2012

Types Of Functions


Types Of Functions
The user defined functions may be classified in the following three ways based on the formal arguments passed and the usage of the return statement, and based on that, there are three types of user defined functions, Type 1 to Type 3.
     (a)   A function is invoked without passing any formal argument from the calling    portion of a program and also the function does not return back any value to the called function.
      (b)  A function is invoked with formal arguments from the calling portion of a program but the function does not return back any value to the calling portion.
      (c)  A function is invoked with formal arguments from the calling portion of a program which return back a value to the calling environment.
Type 1   It is the simplest way of writing a user defined function in C++. There is no data communication between the calling portion of a program and a called function block. The function is invoked by a calling environment by not passing any formal argument and the function also does not return back any value to the caller.
     For example,
          #include <iostream.h>
        void main (void)
        {
              void massage (void) / / function declaration
              massage ( ) ; function calling
        }
        void massage (void) / / function definition
        {

                                                                 
     

  / / body of the function
           }
Type 2   The second type of user defined function passes some formal arguments to a function but the function does not return back any value to the caller. It is an one way data communication between a calling portion of the program and the function block.
   For example,
   #include <iostream.h>
   void main (void)
  {
      void power  (int  ,int) ;  / / function declaration
      int x, y ;         
      power (x, y) ;    / / function calling
  }
  void power (int x, int y)
  {



 
              / / body of the function
      / / no value will be transferred back to the caller
  }

 PROGRAM
A program to find the square of its number using a function declaration without using the return statement.
   / /example 4.3
   / /  passing formal arguments and no return statement
   #include <iostream.h>
   void main (void)
   { 
       void square (int) ;  / / function declaration
       int max;
       cout << “enter a value for n ?  \n” ;
       cin >> max;
       for ( int I =0 ; i <= max-1; ++i)
             square (i) ;
    }
    void square (int n) / / function definition
    {
        float value;
        value =n*n;
        cout << “ I = “ << n << “ square = “ << value << endl ;
    }
Output of the above program
   enter a value for n ?
   4
   i = 0  square = 0
   i = 1  square = 1
    i = 2  square = 4
   i = 3  square = 9

Type 3 The third type of user defined function passes some formal arguments to a function from a calling portion of the program and the compound value, if any, is transferred back to the caller. Data are communicated between both the calling portion of a program and the function block.
    For example,
    #include <iostream.h>
   void main (void)
   {
      int output (int, int, char) ; / / function declaration
      int x, y, temp;
      char ch;


       
       temp = output (x, y, ch) ;
  }
  int output (int a, int b, char s)
  {
      int value;


       
       / / body of the function
      return (something) ;
   }
PROGRAM
A program to find the square of its number using a function declaration with the return statement.
      / / using return statement
     #include <iostream.h>
     void main (void)
    {
        float square (float) ;  / / function declaration
        float i, max, value ;
        max = 1.5 ;
        i = -1.5 ;
        while ( I <= max)  {
              value = square (i) ;
              cout << “ I = “ << I << “ square = “ << value <<endl ;
              I = I + 0.5 ;
     }
}
float square (float n) / / function definition
{
    float value ;
    value = n*n ;
    return (value) ;
}
PROGRAM
A program to find the sum of the given three numbers using function declaration with the return statement.
     / / demonstration of return statement
    #include <iostream.h>
void main (void)
{
    float sum (float, float, float) ;  / / function declaration
    float x, y, z;
    cout << “ enter any three numbers \n” ;
    cin >> x >> y >> z ;
    float total ;
    total = sum (x, y, z) ;
    cout  << “ sum of x, y and z = “ << total << endl ;
}
float sum (float a ,float b, float c)  / / function definition
{
    float temp;
    temp = a+b+c;
    return (temp) ;
}
Output of the above program
   enter any three numbers
   1  2  3 
   Sum of x, y and z = 6
PROGRAM
A program to find factorial of a given number using function declaration.
     / /factorial of a given number
    #include <iostream.h>
    void main (void)
    {
        long int fact (int) ;
        int x, n;
        cout << “Enter any integer number” << endl ;
        cin >>n;
        x = fact (n) ;
        cout << “value = “ << n << “and its factorial = “ ;
        cout << x << endl ;
   }
   long int fact (int n) / / function definition
  {
      int value = 1 ;
      if ( n == 1 )
           return (value  ) ;
     else
          {
               for (int I =1 ; i<=n ; ++i)
                     value = value*i ;
                     return (value) ;
           }
   }
Output of the above program
    enter any integer number
    value = 5 and its factorial = 120
   If the entered number is 1, then the function will return a factorial value 1, else, it will multiply the number n times by incrementing it by 1 in every time.
(1)                                                             (2)
Enter value 1                                  Enter value 2
value = 1                                       Value = 1*1
                                                     value = 1*2
                                                     value = 2
(3)
Enter value 3
= 1*1
= 1*2
= 2*3
Value = 6
And so on, it will calculate the factorial for any number.
PROGRAM
A program to find sum of the following series using a function declaration.
  Sum = x-(x3)/3! + (x5)/5! -… (xn)/n! Where x and n are entered from the keyboard.
 / /sum = x- (x3) /3 ! + (x5) / 5 ! -… (xn) / n !
#include <iostream.h>
{
    long int fact (int) ; / / function decleration
    float power (float, int) ;
    float sum, temp, x, pow;
    int sign, i, n;
    long int factyal ;
    cout << “Enter a value for n ? “<< endl ;
    cin >> n ;
    cout << “Enter a value for x ? “<< endl ;
    cin >> x ;
    i = 3 ;
    sum = x ;
    sign = 1 ;
    while (i <= n)  {
             factval = fact (i) ;
             pow = power (x, i) ;
             sign = (-1) *sign ;
             temp = sign*pow/ factval ;
             sum = sum +temp ;
             i = i+2 ;
    }
     cout << “sum of x-(x^3)/3! + (x^5)/5! -... = “ << sum ;
}
long int fact (int max)
{
    long int value ;
    value = 1 ;
    for (int i = 1; I <= max; ++i)  {
          value = value*i ;
    }
    return (value) ;
}
float power (float x, int n)
{
       float value2 ;
       value2 = 1 ;
       for ( int j = 1 ; j <= n; ++j)
             value2 = value2 * x;
             return (value2) ;
}
Output of the above program
      Enter a value for n ?
      7
      Enter value for x ?
      1
      Sum of x-(x^3)/3! + (x^5)/5! _... = 0.841468

PROGRAM
A program to find the sum of a given non-negative integer of n numbers where n is defined by the programmer using a function declaration.
    Sum= 1+2+3+4…n
     / / sum = 1+2+3…n using non-recursive function
    #include <iostream.h>
    vid main ( void)
 {
        int sum (int) ;
        int x,n;
        cout << “Enter any integer number” << endl;
        cin >> n;
        cout << “value = “ << n << “ and its sum = “ << sum(n) ;
  }
  int sum (int n)
  {
    int value = 0 ;
    if (n == 0)
         return (value) ;
     else
          {
          for (int I = 1; i <= n; ++i)
               value += i;
               return (value) ;
          }
  }
Output of the above program
    Enter any integer number
   10
    Value = 10 and its sum = 55

No comments:

Post a Comment