Function with argument by reference

Is there any actual difference between these 3 function declarations?

1
2
3
int f(int x) {
return x;
}


1
2
3
int f(int &x) {
return x;
}


1
2
3
int f(const int &x) {
return x;
}
There is no difference in what they do. There will be a difference in the code generated by the compiler between 1 and 2 or 3.
int f(int x) ;
Can be called by passing any integer expression - lvalues or rvalues, const or non-const.
The function gets a copy of an int.
It can modify the copy that it received; but the changes are local to its copy.

int f( int& x ) ;
Can only be called by passing a modifiable lvalue of type int.
The function gets an an alias for a modifiable int.
It can modify the aliased int through x; changes are made on the aliased object.

int f( const int& x ) ;
Can be called by passing any integer expression - lvalues or rvalues, const or non-const.
The function gets an an alias for a non-modifiable int.
It can't modify the int through x; it is an alias for a non-modifiable int.
Topic archived. No new replies allowed.