I vaguely understand the difference, and from my basic understanding, generally speaking, it seems like a better idea to call-by-reference than value in most cases. Is this true? Also when is call-by-reference to a constant more ideal than just a call-by-reference?
In the code below, is it *better*/ more sensible to call b by-reference rather than by-value?
[highlight="cpp"]#include <iostream>
#include <string>
using namespace std;
void myfunctionb(string &b) {
cout << b << endl;
}
void myfunctiona() {
string a;
cout << "Enter a string: ";
cin >> a;
myfunctionb(a);
}
int main() {
myfunctiona();
system("PAUSE");
return 0;
}[/highlight]
Call-by-reference vs. Call-by-value
Started by John, Jan 18 2008 09:39 PM
3 replies to this topic
#1
Posted 18 January 2008 - 09:39 PM
|
|
|
#2
Posted 19 January 2008 - 12:35 AM
I wouldn't call b by-reference in that example, I would simply do it by-value. When you're using references you're giving the exact variable to the function, and not only the value. And in your example myfunctionb doesn't need the variable, it only need the value.
Take a look at this sample code.
Take a look at this sample code.
#include <iostream>
void ByValue(int X)
{
X++;
std::cout << X << std::endl;
}
void ByReference(int &X)
{
X++;
std::cout << X << std::endl;
}
int main()
{
int A = 10;
int B = 20;
std::cout << A << std::endl;
std::cout << B << std::endl;
std::cout << std::endl;
ByValue(A);
ByReference(B);
std::cout << std::endl;
std::cout << A << std::endl;
std::cout << B << std::endl;
return 0;
}
As you see, when you're modifying a reference to a variable in a function, the modifications will also be done to the variable when you come back into main, because it's exactly the same variable you received in the function.
#3
Posted 19 January 2008 - 09:26 AM
haha, your example is more-or-less the exact example they gave in my book.
Indeed calling-by-value does only pass the value to the function, but once that value is passed in, the function creates another variable with that value. So in my example above, if I were to call it by value, I would just be creating another copy of b thats not needed. No?
Indeed calling-by-value does only pass the value to the function, but once that value is passed in, the function creates another variable with that value. So in my example above, if I were to call it by value, I would just be creating another copy of b thats not needed. No?
#4
Posted 19 January 2008 - 09:38 AM
Call by reference occurs when you pass the address of the variable to the function (receiving a pointer or reference). It is useful if you need to be able to change the variable's value in the function, or if the variable is extremely large and passing it by value would be extremely inefficient (because of the cost of creating a local copy).
Call by value is when you create a local copy of the variable. This is usually only a good idea for small variables that you will not need to change.
In C++, you can also receive a const reference to make it clear that a function is dealing with the actual data, but will not be making changes to it. This is useful when working with a large struct/class where creating a copy would not work well, but you don't want to accidentally change the data.
Call by value is when you create a local copy of the variable. This is usually only a good idea for small variables that you will not need to change.
In C++, you can also receive a const reference to make it clear that a function is dealing with the actual data, but will not be making changes to it. This is useful when working with a large struct/class where creating a copy would not work well, but you don't want to accidentally change the data.


Sign In
Create Account

Back to top










