Hello all I have a very basic question while learning to code in c++.
I am reading about the difference between pass by reference and pass by value. I wrote a simplistic code to test it but it does something I didn't expect.
#include <iostream>
using namespace std;
void MyIncr (float *x);
int main() {
float score = 10.0;
cout << "Orignal Value = " << score << endl;
MyIncr(&score);
cout << "New Value = " << score << endl;
}
void MyIncr (float *x) {
*++x;
}
How come I get 10 for both couts? However if I change the function to be something like:
void MyIncr (float *x) {
*x += 1;
}
I get 10 for the old value and 11 for the new, which is what I would have expected in the previous case as well.