Section 9.8: Changeable parameters implemented by pointers (Frame 2)                     [prev][home][next]

The reason is that the parameter x in the function increment is a simple int and C does not permit int parameters to be changeable, i.e. to reflect back in their caller any changes to those parameters. We say parameters in C are call by value.

Just to contrast for you, the following C++ program does permit its int parameter to be changeable. Note the additional & symbol after int in the parameter declaration:

void increment (int& x) {               // This is legal C++
     x = x + 1;   // similar to the x++ operation
}
int main() {
     int z = 5;
     increment(z);
     cout << z;   //   shows 6
}

cout << z; is the usual way of writing an output statement in C++, though printf can still be used.

C++ arranges for changeable parameters to push changes to them back to the caller. C forces you to do this explicitly through pointers. Though C++ uses pointers, too, it does so behind the scenes.