The first method passes n by value, i.e. a copy of n is sent to the function. The second one passes n by reference which basically means that a pointer to the n with which the function is called is sent to the function.
For integral types like int it doesn't make much sense to pass as a const reference since the size of the reference is usually the same as the size of the reference (the pointer). In the cases where making a copy is expensive it's usually best to pass by const reference.
Firstly, there is no concept of cv-qualified references. So the terminology 'const reference' is not correct and is usually used to describle 'reference to const'. It is better to start talking about what is meant.
$8.3.2/1- "Cv-qualified references are ill-formed except when the
cv-qualifiers are introduced through the use of a typedef (7.1.3) or
of a template type argument (14.3), in which case the cv-qualifiers
are ignored."
Here are the differences
$13.1 - "Only the const and volatile type-specifiers at the outermost
level of the parameter type specification are ignored in this fashion;
const and volatile type-specifiers buried within a parameter type
specification are significant and can be used to distinguish
overloaded function declarations.112). In particular, for any type T,
“pointer to T,” “pointer to const T,” and “pointer to volatile T” are
considered distinct parameter types, as are “reference to T,”
“reference to const T,” and “reference to volatile T.”
void f(int &n){
cout << 1;
n++;
}
void f(int const &n){
cout << 2;
//n++; // Error!, Non modifiable lvalue
}
int main(){
int x = 2;
f(x); // Calls overload 1, after the call x is 3
f(2); // Calls overload 2
f(2.2); // Calls overload 2, a temporary of double is created $8.5/3
}
The important difference is that when passing by const reference, no new object is created. In the function body, the parameter is effectively an alias for the object passed in.
Because the reference is a const reference the function body cannot directly change the value of that object. This has a similar property to passing by value where the function body also cannot change the value of the object that was passed in, in this case because the parameter is a copy.
There are crucial differences. If the parameter is a const reference, but the object passed it was not in fact const then the value of the object may be changed during the function call itself.
E.g.
int a;
void DoWork(const int &n)
{
a = n * 2; // If n was a reference to a, n will have been doubled
f(); // Might change the value of whatever n refers to
}
int main()
{
DoWork(a);
}
Also if the object passed in was not actually const then the function could (even if it is ill advised) change its value with a cast.
e.g.
void DoWork(const int &n)
{
const_cast<int&>(n) = 22;
}
This would cause undefined behaviour if the object passed in was actually const.
When the parameter is passed by const reference, extra costs include dereferencing, worse object locality, fewer opportunities for compile optimizing.
When the parameter is passed by value and extra cost is the need to create a parameter copy. Typically this is only of concern when the object type is large.
There are three methods you can pass values in the function
Pass by value
void f(int n){
n = n + 10;
}
int main(){
int x = 3;
f(x);
cout << x << endl;
}
Output: 3. Disadvantage: When parameter x pass through f function then compiler creates a copy in memory in of x. So wastage of memory.
Pass by reference
void f(int& n){
n = n + 10;
}
int main(){
int x = 3;
f(x);
cout << x << endl;
}
Output: 13. It eliminate pass by value disadvantage, but if programmer do not want to change the value then use constant reference
Constant reference
void f(const int& n){
n = n + 10; // Error: assignment of read-only reference ‘n’
}
int main(){
int x = 3;
f(x);
cout << x << endl;
}
Output: Throw error at n = n + 10 because when we pass const reference parameter argument then it is read-only parameter, you cannot change value of n.
The first method passes
n
by value, i.e. a copy ofn
is sent to the function. The second one passesn
by reference which basically means that a pointer to then
with which the function is called is sent to the function.For integral types like
int
it doesn't make much sense to pass as a const reference since the size of the reference is usually the same as the size of the reference (the pointer). In the cases where making a copy is expensive it's usually best to pass by const reference.Firstly, there is no concept of cv-qualified references. So the terminology 'const reference' is not correct and is usually used to describle 'reference to const'. It is better to start talking about what is meant.
Here are the differences
The important difference is that when passing by
const
reference, no new object is created. In the function body, the parameter is effectively an alias for the object passed in.Because the reference is a
const
reference the function body cannot directly change the value of that object. This has a similar property to passing by value where the function body also cannot change the value of the object that was passed in, in this case because the parameter is a copy.There are crucial differences. If the parameter is a
const
reference, but the object passed it was not in factconst
then the value of the object may be changed during the function call itself.E.g.
Also if the object passed in was not actually
const
then the function could (even if it is ill advised) change its value with a cast.e.g.
This would cause undefined behaviour if the object passed in was actually
const
.When the parameter is passed by const reference, extra costs include dereferencing, worse object locality, fewer opportunities for compile optimizing.
When the parameter is passed by value and extra cost is the need to create a parameter copy. Typically this is only of concern when the object type is large.
With
n
is a copy of the value of the actual parameter, and it is legal to change the value ofn
within the function. Withn
is a reference to the actual parameter, and it is not legal to change its value.There are three methods you can pass values in the function
Pass by value
Output: 3. Disadvantage: When parameter
x
pass throughf
function then compiler creates a copy in memory in of x. So wastage of memory.Pass by reference
Output: 13. It eliminate pass by value disadvantage, but if programmer do not want to change the value then use constant reference
Constant reference
Output: Throw error at
n = n + 10
because when we pass const reference parameter argument then it is read-only parameter, you cannot change value of n.Since none of you mentioned nothing about the const keyword...
In other words: passing a parameter by reference exposes it to modification by the callee. Using the const keyword prevents the modification.