The answer is in where a variable exists and where it doesn't.
When you created
double F;
in main, an 8-byte space was created in memory to store numbers. This space is main's F ... you can create NO other variables named F in main.
Then you did
cin >> F;
, inputted a number from the user into main's F variable.
By specifying your function with
double convertTemp(double F)
you are telling this function to create an 8 byte memory space called F, and that the coder must give this function a number to fill that variable up.
When you called the function and passed main's F into the parenthesis
double C = convertTemp(F);
, at that time "convertTemp's F" is created, and the number you passed "main's F" is copied into that space. so if 32 was in main's F .... then convertTemp's F is created and has 32 copied into it when the function is called.
Then
double C;
is created in your function. So there is a C in the function being filled up, and a C in main waiting to be filled when the function returns. So you have two 8 byte F variables, and two 8 byte C variables in memory at this point.
Then you
return C;
. So main's C has the value in convertTemp's C copied into it. After the return all the variables created by the function (convertTemp's F and convertTemp's C) are removed from memory, but thats ok because we already got our answer copied into main's variable C.
-------------
If you change the names of your variables in the function it might make more sense that they are not the same... you could change the function this way and it will still work perfectly, since the names don't matter:
1 2 3 4 5 6
|
double convertTemp(double Fahrenheit)
{
double Celsius;
Celsius = (Fahrenheit-32.0) * (5.0/9.0);
return C;
}
|
All the compiler cares about is that it knows what data type it is (double, 8 bytes) and what to call it (F, Fahrenheit, Jimmy,...whatever).