Using structure as a function parameter

Program 50) Write a simple program to illustrate the method of sending an entire structure as a function to a function.
A program to update an item as shown below. The function updates receives a copy of the structure variable item as one of its parameters. Note that both the function update and the formal parameter product are declared as type struct stores. It is done so because the function uses the parameter product to receive the parameter product to receive the structure variable item and also to return the updated values of item.

The function mul is of type float because it returns the product of price abd quantity. However, the parameter stock, which receives the structure variable item is declared as type struct stores.  
  The entire structure returned by update can be copied 
into a structure of identical type. The statement
                 item = update (item, p_increment, q_increment) ;
replaces the old values of item by the new ones.

                                  PROGRAM
/*         Passing a copy of the entire structure        */
   struct      stores
   {
            char name [20] ;
            float price ;
            int  quantity ;
  } ;
 struct stores update (struct stores product, float p, int q) ;
 float mul  (struct stores stock) ;
 main ( )
  {
        float   p_increment, value ;
        int      q_increment ;

       struct stores item = {"XYZ", 25.75, 12) ;

      printf ("\nInput Increment values:") ;
      Printf ("    price increment and quantity increment\n") ;
      scanf ("%f %d", &p_increment, &q_increment) ;

/*  ----------------------------------------------------------------------*/
       item = update (item, p_increment, q_increment) ;
/*  ----------------------------------------------------------------------*/
      printf ("Updated values of item\n\n") ;
      printf ("Name        : %s\n", item.name) ;
      printf ("Price         : %f\n", item.price) ;
      printf ("Quantity   : %d\n", item.quantity) ;

/*  ---------------------------------------------------------------------*/
      Value = mul (item) ;
/*  --------------------------------------------------------------------*/
      Printf ("\nvalue of the item = %f\n", value) ;
 }
 struct stores update (struct stores product, float p, int q)
 {
          product.price += p ;
          product.quantity += q ;
          return (product) ;
 }
 float mul (struct stores stock)
 {
       return (stock.price * stock . quantity) ;
 }

                              Output    

Input increment values : price increment and quality
                                                                                                                  increment
10  12
Updated values of item
NAME      :  XYZ
Price        :  35.750000
Quantity  :  24
Value of the item = 858.000000


You may notice that the template of stores is defined before main( ). This has made the data type struct stores as global and has enabled the functions update and mul to make use of this definition.

No comments:

Post a Comment