Rectangle: Click to edit Master text styles
Second level
Third level
Fourth level
Fifth level
Using reference variables in functions
w#include <iostream.h>
wvoid times2(int &x); // function prototype
wvoid main() {
w int var; // declare var as integer variable
w var = 10; // put value of 10 in var
w cout << "var is " << var << endl;
w times2(var); // call 'times2()' with var as parameter
w cout << "var is now " << var << endl;
w}
wvoid times2(int &x) { x = x * 2; }
The point of reference variables and functions is that you can pass a variable as a parameter and have the variable changed in the function. 
In the above example, whatever we did to x in the function 'times2()' would effect the actual variable we passed to the function. In this case we multiplied x by 2. We passed var to times2(). So x becomes a reference to var when we called it. Now we make x equal itself times 2. And since we passed var to the function it multiplies THAT by two.