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 }
4 Answers
+ 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.
0
Ace thank you but what will be the difference if we change that & to * ?? like this :
void square(int * ptr) ???