I am well aware that modifying a function\'s argument that is passed by value is ineffective outside of the C/C++ function, but compilers allow it - but what happens? Is a loca
As others have explained, x
is local in function doSomething
, any modifications only affect the local copy of the argument.
Note however that C++ allows passing by reference: a very small change in the definition of doSomething()
would have significant consequences for this program:
void doSomething( int& x ) {
x = 42;
printf( "The answer to Life, the Universe and Everything is (always): %i!\n", x );
}
With the above function definition, variable a
in main
would indeed have its value changed to 42
. Since the code in main()
is identical, this C++ feature can lead to confusing code, especially for a C programmer.