Why does call-by-value example not modify input parameter?

c++, call

Solution

When you pass a function argument by value a copy of the object gets passed to the function and not the original object.Unless you specify explicitly arguments to functions are always passed by value in C/C++.

Your function:

void changeValue(int value)

receives the argument by value, in short a copy of `value` in `main()` is created and passed to the function, the function operates on that value and not the `value` in `main()`.

If you want to modify the original then you need to use pass by reference.

void changeValue(int &value)

Now a reference(alias) to the original `value` is passed to the function and function operates on it, thus reflecting back the changes in `main()`.

Problem

In the following call-by-value example, I cannot understand why this code is not changing the value of the 5 to a 6. Line 11 calls the function changeValue which has the value 6, so I would have thought 6 should be output, however 5 is still output? ``` #include <iostream> using namespace std; void changeValue(int value); int main() { int value = 5; changeValue(value); cout << "The value is : " << value << "." << endl; return 0; } void changeValue(int value) { value = 6; } // This doesn't change the value from 5 to 6. 5 is output? ```

Original source