Passing Variables by Reference

I was going through the c++ tutorial form this website, the pdf version, and came to the functions section. It talked about passing variables by reference. I believe that the following script would print 4, right? If not, can you tell me what I'm doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//prefix(es)
#include <iostream>
using namespace std;

void transferer(int&a) //function declaration
{
a=a*2 //simply takes the number, 2 in this case, and multiplies it by 2 to get four.  
}
main() //main code
{
int variablea=2; //defines my variable to be changed to a value of 2
transferer(variablea); //calls the funciton
cout<<variablea; //prints four on the screen
}
yes that's right. The main difference between passing by value and by reference is that by passing by reference you can make changes to the original inside of the function, but when passing by value the function makes a copy of the original and can only change that. You seem to have the idea down just fine =)
Thanks! Glad to know I've got the right idea.
-Ameobea
Topic archived. No new replies allowed.