@Tom Backton
hmm I'm not sure what you're trying to say, maybe some code examples would be understood much better. I think what you're trying to say though is that pass by value parameters get changed? Actually pass by value parameters don't get changed since their scope is only within the function. Well for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// example of wrong thing to do
#include <iostream>
#include <limits>
void doubleIt(int num)
{
num = num * 2;
}
int main()
{
int myNum = 2;
doubleIt(myNum); // pass by value WRONG doesn't double myNum
std::cout << myNum << std::endl; // prints out 2
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
|
It would print out 2 on your console. Why? Since the parameter is actually a local variable and it's scope is only within the function.
When you pass something in by value the function actually makes a copy. This copy isn't the variable you passed into the function but a new variable.
The copy is what the function does it's operations on.
when the function ends all the variables or parameters inside the function is freed up automatically. Hence the copy would be freed up and everything you did with it would be gone as well.
One way around this, is to return the value of the copy variable back to the original variable just like Bazzy shown in his code. So in our example we could fix it by making a few changes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
//solution using return
#include <iostream>
#include <limits>
int doubleIt(int num)
{
return num * 2;
}
int main()
{
int myNum = 2;
myNum = doubleIt(myNum); // pass by value returns result to original
std::cout << myNum << std::endl; // prints out 4
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
|
Now the program would print out 4 which is the correct value. However, remember the thing about the copy variable? We had to make a copy which means that instead of just having one integer variable to operate on we needed 2 integer variables. This may be ok since integers take up only a few bytes of memory. It's pretty inefficient when you're working with classes or structures that take up more memory. Which brings us to another solution.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// pass by reference
// The C solution using pointers
#include <iostream>
#include <limits>
void doubleIt(int *num)
{
*num = (*num) * 2;
}
int main()
{
int myNum = 2;
doubleIt(&myNum); // C-style pass by reference also works in C++
std::cout << myNum << std::endl; // prints out 4
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
|
And the C++ way of passing by reference
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
// pass by reference
// The C++ solution using references
#include <iostream>
#include <limits>
void doubleIt(int &num)
{
num = num * 2;
}
int main()
{
int myNum = 2;
doubleIt(myNum); // C++ style pass by reference
std::cout << myNum << std::endl; // prints out 4
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
|
the main advantage of passing by reference is it doesn't make a copy variable, saving you a few bytes. When you pass by value on the other hand, you can never save a few bytes because of the copy variable. I hope that made it clearer.