0

Help

For the Code Below Please someone explain How the value of variable 'number' bcame 64 when we print it for the second time? #include <iostream> using namespace std; void square(int&ptr){ ptr=ptr * ptr; } int main() { int number=8; cout<<number<<endl; square(number); cout<<number; return 0; any help is appreciated..thank you }

7th Apr 2018, 1:50 PM
RiGeL
RiGeL - avatar
2 Réponses
+ 5
When you using reference &, you use the address of the object, that's why the function directly operates on the variable and this address,NOT copy. If you passed the variable by the value in the function, the created copy would perform the task and and will be destroyed (copy variable) and function would return the variabels unchaged.
7th Apr 2018, 2:23 PM
Andrew
Andrew - avatar
0
Ace thank you but what will be the difference if we change that & to * ?? like this : void square(int * ptr) ???
7th Apr 2018, 2:13 PM
RiGeL
RiGeL - avatar