User defined functions

This is a discussion on User defined functions within the C Programming forums, part of the General Programming Boards category; I am instructed to do the following: Add a user defined function which does the calculation involving the minor product ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    19

    User defined functions

    I am instructed to do the following:

    Add a user defined function which does the calculation involving the minor product code. It should be passed the minor part of the product_code and the base_price, and should return the (possibly) modified price.Add a second user defined function which does the calculation involving the major product code. It should be passed the major part of the product_code and the price calculated up to that point, and should return the (possibly) modified price.Add a third user defined function which does the calculation involving the shipping. It should be passed the "where" part of the product_code and the price calculated up to that point, and should return the final price.
    However, I'm not sure what the assignment means. Firstly, is "passed" supposed to be "past"?

    Also, what does it mean to "return a possibly modified price"? Can anyone give me an example of how to use functions?

    Here is my original code (which by the way, isn't even running due to the problem i mention here
    Code:
    #include <stdio.h>
    
    int main()
    {
    
           int prod_num, number, counter=0, major_code=0, minor_code=0,
    tax_code=0, where=0, total_number=0;
           double base_cost, minor_factor=0, shipping_costs=0, tax_amount=0,
    final_unitprice=0,average_final, final_subtotal=0, final_price_product;
    
           while ( scanf("%d %lf %d", &prod_num, &base_cost, &number) != EOF)
           {
                   total_number += number;
    			   counter++;
                   major_code = prod_num / 100000;
                   minor_code = (prod_num % 100000) / 100;
                   tax_code = (prod_num % 100) / 10;
                   where = prod_num % 10;
    
                   if(minor_code >= 0 && minor_code <= 399) {
                           minor_factor = 1.45;
                   }
    
                   else if ((minor_code>=400 && minor_code <=799) || (minor_code>=950
    && minor_code <=955)) {
                           minor_factor = 1.12;
                   }
    
                   else {
                           minor_factor = 0.89;
                   }
    
                   switch(where)
                   {
                           case 0:         shipping_costs = 7.50;
                                                   break;
    
                           case 1:         shipping_costs = 8.47;
                                                   break;
    
                           case 2:         shipping_costs = 6.05;
                                                   break;
    
                           case 3:         shipping_costs = 10.20;
                                                   break;
    
                           default:        shipping_costs = 20.00;
                                                   break;
    
                   }
    
                   if(tax_code == 1)
    
                           {
    
                           if(major_code>=0 && major_code<=250)
                                   tax_amount = 0.12;
    
                           else if(major_code>=251 && major_code<=450)
                                   tax_amount = 0.15;
    
                           else
                                   tax_amount = 0.25;
    
                           }
    
                   else if (tax_code == 0)
                           tax_amount = 0;
    
                   final_unitprice = (base_cost * minor_factor + shipping_costs) * (1 + tax_amount);
                   final_price_product = final_unitprice * number;
    			   final_subtotal += final_price_product;
    			   
                   printf("---------------------------> PRODUCT [%d]<---------------------------\n", counter);
                   printf("\n");
                   printf("Product #: %d || Price/Unit: $%.2f || # of Products: %d || 
    Total: $%.2f\n", prod_num, final_unitprice, number, final_price_product);
                   printf("\n");
                   printf("\n");
    
           }
    
    average_final = final_subtotal / total_number;
    
    		printf("\n");
    		printf("The total price is: $%.2f\n", final_subtotal);
    		printf("The average price per product is: $%.2f", average_final);
    		printf("\n");
    		
           return 0;
    }

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Firstly, is "passed" supposed to be "past"?
    No. If you don't know the difference between the two words, then you should look them up on any standard American English disctionary. past means at some previous time, like yesterday. passed means to give some object from one person to another -- "I passed the ball to you". In the sense of the function, variable values are passed from one function to another.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    19
    Quote Originally Posted by Ancient Dragon
    No. If you don't know the difference between the two words, then you should look them up on any standard American English disctionary. past means at some previous time, like yesterday. passed means to give some object from one person to another -- "I passed the ball to you". In the sense of the function, variable values are passed from one function to another.
    Okay..

    Yet i still don't understand how "variable values are passed from one function to another".


    Anyways, i understand this task in the following fashion. I replace all the main parts of the program with e.g. "func_name1()" and define that function at the end of the code. Correct?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. user defined header files
    By sidu in forum C++ Programming
    Replies: 1
    Last Post: 07-11-2008, 06:40 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Instant messenger app help
    By AusTex in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 12:41 AM
  4. Piecewise defined discontinuous functions in Mathematica
    By SourceCode in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 03-16-2005, 12:24 PM
  5. functions defined as a string
    By river-wind in forum C Programming
    Replies: 2
    Last Post: 11-21-2001, 09:36 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21