Functions with arguments 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;
}
Yes, the parameter types are different.
they look different, duh! but i don't see any functional difference when using either of them
You asked the same question here:
http://www.cplusplus.com/forum/beginner/145815/
JLBorges gave you an excellent answer.

Why are you asking the same question again?
Last edited on
you created same topic twice at the same time. other topic answers are right.

but, only the above code snippets isn't meaningful,
because from another function, int n = f(5); why? instead int n=5;

they will be meaningful if expanded,
1
2
3
4
5
int f(int x) {
x++; // change doesn't effect in caller function
cout << x;
return 5;
}


1
2
3
void f(int &x) {
x=x+2; // change effects in caller function
}


1
2
3
4
int f(const int &x) {
--x; // illegal, you cant change x
return x;
}
Oh, sorry for the double post, I didn't even see there was a 2nd question created. No idea why that happened
Topic archived. No new replies allowed.