+ 1

Reference parameters in C++

What happens when we define one reference parameter in function definition and send two parameters from function call Example ~~~~~~~ void location (int &x, int y=4) { y+=2; x+=y; } int main () { int px=10, py=2; location (px); location (px,py); // my doubt is this function call. cout<<px<<py; }

8th Aug 2017, 12:37 PM
Raj Luffy
Raj Luffy - avatar
6 Antworten
+ 6
(py) value replaces the 2nd parameter's default value and nothing happens to (py) value because it's not being sent to the function by reference. output: 14 2
8th Aug 2017, 12:48 PM
Jihad Naji
Jihad Naji - avatar
+ 7
Your parameter will just take the value of py. Since the py is passed by value, no changes will be made to py.
8th Aug 2017, 12:49 PM
Hatsy Rei
Hatsy Rei - avatar
+ 5
yup instead of 4, 2 is passed to the function
8th Aug 2017, 12:52 PM
Jihad Naji
Jihad Naji - avatar
+ 2
then in second function call py value is replaced?!!
8th Aug 2017, 12:51 PM
Raj Luffy
Raj Luffy - avatar
+ 2
yes thanks I understood the logic now thank u
8th Aug 2017, 12:53 PM
Raj Luffy
Raj Luffy - avatar
+ 2
it doesnt give that output : 14 2
27th Apr 2019, 1:30 AM
Gustave A C/D C ☢️ 🛸🛸🛸
Gustave A C/D C ☢️ 🛸🛸🛸 - avatar