Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

void function question. please help

Status
Not open for further replies.

nickadeemus2002

Programmer
Feb 27, 2004
9
US
good morning. I have the following question.

I am using a void function to dispaly a menu template that has a specific output format. The function is a void function with the following handle:

void productSelection()

My question is...is it possible to pass reference values into the the void function that will update variables withing the menu template?

For example, if I create this function:

void productSelection( double &drinksCost)
{
.
.
cout<<"The total cost for supplying drinks is"<<drinksCost;
.
.
}

will this work? My goal is to create a function that will display updated data to the screen in a specific format.

Please advise

 
Yes... you can have multiple functions by the same name with different parameters. I think that is what you are asking

Matt
 
Thanks for the help matt. I am sorry if I was unclear. Let me post the function example so you can see if it is valid
Code:
void menu(double &sandwichQty, double &sandwichCost,...)
{    			        	//create menu interface.
    
    cout<<"**************************************"<<endl;
    cout<<"*******  Snack Bar Register  *********"<<endl;
    cout<<"****************************************"<<endl;
    cout<<endl;
    cout<<endl;
    cout<<"\t  S -       SANDWICH       - $3.00"<<endl;
    cout<<"\t  C -        CHIPS         - $1.50"<<endl;
    cout<<"\t  D -     LARGE DRINK      - $2.00"<<endl;
    cout<<"\t  T - Calculate Total Sale -  $$$"<<endl; 
    cout<<"\t  X - Cancel Sale and Start Over - ";
    cout<<endl;
    cout<<endl;
    cout<<"****************************************************"<<endl;
cout<<"*******************************************"<<endl;
cout<<"Item Purchased"<<"\t Quantity"<<"\t SaleAmount"<<endl;
cout<<"********************************************"<<endl;

cout<<"Sandwich"<<'\t'sandwichQty<<'\t'sandwichCost;
etc
etc

What I am trying to do is use the menu function to accept data from other functions that calculate accumulator totals and pass that data by reference to the menu() which has a specified format. I want to use the menu() just for displaying and updating the data. Is this a valid function?


thanks for the help
 
There are several ways in which you can do this ...
[ol]
[li]Pass by value:

void menu (double sandwichQty, double sandwichCost);

If what you re trying to do is just to display the variables, this approach is perfectly fine. However, this will not work if your function is going to alter the variables and other functions are going to use your altered variables.

[/li]

[li]Pass by reference or pointer:

void menu (double &sandwichQty, double &sandwichCost);
void menu (double *sandwichQty, double *sandwichCost);

These approaches usually indicate that you are going to alter the variables (parameters) within your function (eg. changing their values). However, this is not always the case. Some people just prefer to pass the actual objects rather than the copies of the objects. If you are going to use these approaches but without altering the variables inside your function, I recommend using the "const" for clarity. Thus,

void menu (const double &sandwichQty, const double &sandwichCost);

[/li]

[li]Use global variables:

{
double sandwichQty;
double sandwichCost;

...//other functions declarations here
void menu ();
}

In this case, your function menu can use and alter the variables. Although the practice of using global variables is usually shunned by OO practitioners, this approach avoids the push and pop operations and thus make your program a bit faster (especially if your program is going to use the function intesively and repeatedly).[/li][/ol]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top