"&" what it means

I,m a beginner and i saw this on a youtube tutorial. I saw the tutorial to learn more about the void. But i ended the tutorial questioning myself why there is a "&" at totaltip and total and not at bill tip or people.
Can someone help me?
And i appreciate to if someone tell me about voids at a simple way since i'm not very familiar with english.

#include <iostream>
using namespace std;

void tipcalc (int people, float bill, float tip, float& totaltip, float& total);

int main ()
{
int people;
float bill, tip, totaltip, total;
cout << "How much is the bill? ";
cin >> bill;
cout << "How many people? ";
cin >> people;
cout << "What's the percentage of the tip (per person)? "; //Discount
cin >> tip;

tipcalc (people, bill, tip, totaltip, total);

getchar ();
cout << "The total tip at " << tip << "% is " << totaltip <<"EUROS." << endl;
cout << "The total is " << total << "." << endl;
getchar ();
return 0;

}

void tipcalc (int people, float bill, float tip, float& totaltip, float& total)
{

totaltip = bill * (tip/100);
total = (totaltip + bill) / people;

}
It means the function parameters are passed by reference. Please see the tutorial page on this topic:
http://www.cplusplus.com/doc/tutorial/functions2/
@chervil
Hmm, so let me see, if we didn't put the "&" on totaltip, totaltip wouldn't have any value, and the same with total?
Well, the same variable names are used in both function main() and in function tipcalc().

But the variable named totaltip can exist independently in each function. If the & is not used, the argument is passed by value. What that means is that the function tipcalc() would receive a copy of whatever value the variable had prior to the function call. So even though the variable has the same name, it is a completely separate object which has its own independent existence.

Inside function tipcalc(), the variable is used, and given a particular value. But it is a local variable, existing only within the function. When the function terminates, the local variables are discarded. The original variable in function main() would remain unaltered, since the only thing that was done was to make a copy of its value.

On the other hand, when passed by reference using the &, the variable inside the function tipcalc() is effectively the same object as the variable in main(). This time there are not two separate variables, there is just one, so whatever is done to it in the function will naturally be reflected in the variable after returning to main().
Last edited on
totaltip wouldn't have any value

No. Without the & totaltip and total would be passed by valie (i.e. a copy). Any changes made to the copy would not be seen in the calling function. When passing by reference you're telling the compiler to use/update the value in the calling function.
Thank you so much :D
Now I have a better vision of references :)
Here is the final result :)

#include <iostream>
using namespace std;

void tipcalc (int people, float bill, float tip, float& totaltip, float& total) // "&" in both
{

totaltip = bill * (tip/100);
total = (totaltip + bill) / people;

}

int main ()
{
int people;
float bill, tip, totaltip, total;
cout << "How much is the bill? ";
cin >> bill;
cout << "How many people? ";
cin >> people;
cout << "What's the percentage of the tip (per person)? "; //Discount
cin >> tip;

tipcalc (people, bill, tip, totaltip, total);

getchar ();
cout << "The total tip at " << tip << "% is " << totaltip <<"EUROS." << endl;
cout << "The total is " << total << "." << endl;
getchar ();
return 0;

}
Last edited on
Topic archived. No new replies allowed.