The program only considers what is in a particular scope, for example:
void PrintDifficulty(int Difficulty)
{
// Once here, this scope knows nothing about the variable "LevelDifficulty"
std::cout << Difficulty;
}
int main()
{
int LevelDifficulty = 2;
int OtherDifficulty = 3;
// This scope knows nothing about the variable "Difficulty"
PrintDifficulty(LevelDifficulty);
// When calling the function, you provide the value of "LevelDifficulty"
// In the PrintDifficulty function, it is called "Difficulty" because that is
// how it was named in the argument list of the function.
PrintDifficulty(OtherDifficulty);
// Now, you've provided a different value from a different variable, but
// in the function, it is still called "Difficulty"
}
There are some exceptions to this, for example, if your variables were in the global scope, then you could also use them in the function, but that is not a good way to do it. Function calls are maintained on what is called the “stack”, when you move into another scope, your computer creates a new memory space (called a stack frame) for working with the variables in that scope. Once you leave the function, the memory space is deleted from the stack. If you’re using Visual Studio, you can put a break point in your function and you will be able to see the stack and you can analyze the contents of variables in each:
Note here I have double clicked on the call stack and it’s showing me the stack frame UNDER the break point frame, here there are two variables:
When I go to the stack frame at the top of the stack, there is only one variable:
When I click continue I will end up in the same place, but a new stack frame with the OtherDifficulty value instead:
If I had a break point in the main between the two function calls, you would see that the stack would be only one high again, before entering the function a second time, creating a new frame at the top of the stack again.
Note also the line listed in the screenshots for the main function stack, once you return from the function the program will continue execution at the line listed there, so in the first two screenshots returning will continue execution on line 14, in the next one, once I return from the function the program will continue on line 15.