Thread: passing functions

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    34

    passing functions

    Hello,
    Could somebody tell me how to pass something from one function to another? For example, say I had 3 functions, one to get two numbers, one to add the two numbers together, and one to take that total and add tax to it. I'm trying to understand how to take the total from adding 2 numbers in one function and returning it to another to add tax to the total from the other function. I hope this makes sense. I'll post what I'm trying to do. This works ok until I try to add the tax function. Thanks. mcorn

    code:--------------------------------------------------------------------------------
    Code:
    #include<iostream.h>
    
    void GetCookieQty(float *,float *);
    float CalcCookiePrice(float,float);
    //float GetTaxes();   //new
    
    #define l_price .95;
    #define r_price .50;
    //#define tx_rate .05;   //new
    
    int main(void)
    {
    	float howmany;
    	float ptr1,ptr2,tax;
    	GetCookieQty(&ptr1,&ptr2);
    	howmany=CalcCookiePrice(ptr1,ptr2);
    //	tax=GetTaxes();   //new
    	return 0;
    }
    
    void GetCookieQty(float *ptr1,float *ptr2)
    {
    
        cout<<"\nHow many large cookies? ";
    	cin>>*ptr1;
    	cout<<"How many regular cookies? ";
    	cin>>*ptr2;
    }
    
    float CalcCookiePrice(float ptr1,float ptr2)
    {
    	float num1,num2,num3;
    	cout.precision(2);
    	cout.setf(ios::showpoint | ios::fixed);
    	num1= ptr1 * l_price;
    	num2= ptr2 * r_price;
    	num3= num1+num2;
    	cout<<"\n"<< num1 << " for large cookies."
    		<<"\n"<< num2 << " for regular cookies."
    		<<"\n\nThe total is " << num3<<endl<<endl;
    	return num3;   //this was return 0;
    }
    
    //float GetTaxes(float num4)   //new
    //{
    //	return num4=num3*tx_rate;
    	//cout<<num3;   //new
    	
    //}
    &#91;code]&#91;/code]tagged by Salem

  2. #2
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Use code tags next time please.

    You will need to pass it in as a parameter:

    Code:
    float GetTaxes(float);
    ...
    tax=GetTaxes(howmany);
    ...
    float GetTaxes(float num4)
    {
       float temp= num4 + num4*tx_rate;
       cout<<temp<<endl;
       return temp;
    }
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  3. #3
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511
    Some quick corrections:

    in C++ we use const to declare constants not #define so
    Code:
    const float i_price .95;
    const float r_price .50;
    Also, standard C++ does not use the .h for header files so
    Code:
    #include <iostream>
    
    using namespace std;

    Also, why are you using pointers - you have not declared any and use pass by reference using &- it will help.

    Also, do you have some book to help with this???

    Mr. C.

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    Thanks for the tips! mcorn

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    34
    Hi,
    I'm using pointers because the book that I'm learning from stated that in order to return 2 or more values I must use pointers. I guess I could have misunderstood the point. If pointers are not right, then how would you return more than one value? Thanks. mcorn

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing from functions in classes to another function in another class?
    By Robert_Sitter in forum Windows Programming
    Replies: 1
    Last Post: 12-13-2005, 07:46 AM
  2. passing 2dimensional arrays to functions
    By owi_just in forum C Programming
    Replies: 1
    Last Post: 04-25-2005, 08:08 AM
  3. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  4. Passing structures between functions
    By TankCDR in forum C++ Programming
    Replies: 2
    Last Post: 01-29-2002, 10:54 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM