I have these two functions one of them using a reference variable and the otherone using a
regular variable and I get the same ecxact result if I pass the same paramter when called.
Instead of having the output inside of the function try writing main like this for both examples you have listed:
1 2 3 4 5 6 7 8 9 10 11
int main(int argc, char *argv[])
{
int number = 6;
func1(number); //YOUR FUNCTION CALL GOES HERE
cout << number;
cin.sync();
cin.ignore();
return EXIT_SUCCESS;
}
The difference is that when you pass a variable into a function by reference you are modifying the variable at it's base address. Any changes you make to that variable inside of that function are reflected on the original. In your second example the system makes a copy of the variable for use within that function, any changes to it are not done to the original variable.
reference basically modifies the original variables value
A reference IS the original variable. It's not like the original, or have some special property that makes it do something to some other variable that it is supposed to mean, it is the actual variable. All a reference is... is another "name" or "label" for some object (variable) that already exists.
Once you see code in this way, things are much clearer. It may take a little getting used to.
Ok, I now understand what a reference variable does but I still don't see where, when or why would you use this type of variables, I cannot think of any unique situation where this would fit.
Can someone be so kind an show me where, when or why a reference variable would be used?
When would you say, now I need a reference variable?
Sorry if my question doesn't make too much sense. Thanks a lot for dealing with this type of questions.
When you're doing stuff like recursive programming passing by reference helps cut down on the amount of overhead caused by having to copy the value to a stack at every iteration of the function. It also cuts down on the amount of typing you have to do since you don't have to explicitly tell the variable to return and keep track of it all of the time.
Then call your functions and then when your function returns, output the input parameter like so. (I know you already said you did this, but you didn't see what he was talking bout so you did something incorrectly.
1 2 3 4 5 6 7 8 9 10 11 12 13
int main(int argc, char *argv[])
{
int number = 6;
func1(number);
cout << number; //Number is 9 here after function returns...
func2(number);
cout << number; //Number is STILL 9 here after function returns...
cin.sync();
cin.ignore();
return EXIT_SUCCESS;
}
When you pass a variable by value, you cannot modify its value inside the function. If you need to be able to modify the value then you would pass by reference.
Without references the following function would not be able to do anything with the firstName and lastName variable inside the function that would live outside the function. By passing by reference you can modify the actual firstName and lastName variable inside the function (because references are just another name for the object). void GetInputData(string &firstName, string &lastName);
Without references the following function would not be able to do anything with the firstName and lastName variable inside the function that would live outside the function. By passing by reference you can modify the actual firstName and lastName variable inside the function (because references are just another name for the object).
void GetInputData(string &firstName, string &lastName);
But would this do the same thing
1 2 3 4 5 6 7 8
void GetInputData(string firstName, string lastName);
{
string firstN = firstName;
string lastN = lastName;
// then use firstN and lastN in the rest of your code
}
If the main use for reference variables is to better manage memory and for less stress to the computer I got it.
I have these two functions one of them using a reference variable and the otherone using a
regular variable and I get the same ecxact result if I pass the same paramter when called.
When you pass by value, your parameter allocated enough memory for the data type passed in. It then creates a copy of whatever variable passed in:
1 2 3 4 5 6
void DoStuff(int a) // Allocate memory for int. Copy whatever is passed in into that allocated slot.
{
a += 5; // Operation performed on that copy
}
// Here, outside of the scope, the copy doesn't exist (or isn't reachable)
Let's put that into some perspective:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
void DoStuff(int a)
{
a += 5;
}
int main()
{
int my_int = 1;
DoStuff(my_int);
// In this example, DoStuff allocated space for an int, refers to it as 'a'.
// Then, it copies the contents of my_int (i.e. 1) into that allocated space.
// It performs operations on the instance in the newly allocated space.
// Meaning operations aren't performed on the variable held at the space allocated to my_int.
cout << my_int; // This will still equal 1
}
If you pass it by reference, you pass the address of the parameter. You don't allocate a new section of memory and all operations are performed on whatever is at the address passed in.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
void DoStuff(int &a)
{
a += 5;
}
int main()
{
int my_int = 1;
DoStuff(my_int);
// In this example, the memory address of my_int is passed into DoStuff.
// In the scope of the function, this is herein referred to as 'a'.
// Any operations are performed directly on the value at that address, not a copy.
cout << my_int; // This will equal 6
}
You use references when you would otherwise use a pointer, but don't want a pointer cause you have references (or can't get a pointer because you're dealing with a temporary value).
or can't get a pointer because you're dealing with a temporary value
this is actually valid (at least for string literals). In most any other case however, if you can get a reference you can get a pointer. References have to be initialized. see below: