|
|
|
|
|
|
|
As an exercise, try reentering the program with other variable names as illustrated here: |
|
|
|
|
|
|
|
|
1: #include <iostream.h>
2:
3: float Convert(float);
4: int main()
5: {
6: float TempFer;
7: float TempCel;
8:
9: cout << Please enter the temperature in Fahrenheit: ;
10: cin >> TempFer;
11: TempCel = Convert(TempFer);
12: cout << \nHere's the temperature in Celsius: ;
13: cout << TempCel << endl;
14: return 0;
15: }
16:
17: float Convert(float Fer)
18: {
19: float Cel;
20: Cel = ((Fer - 32) * 5) / 9;
21: return Cel;
22: return 0;
23: } |
|
|
|
|
|
|
|
|
You should get the same results. |
|
|
|
|
|
|
|
|
New Term: A variable has scope, which determines how long it is available to your program and where it can be accessed. Variables declared within a block are scoped to that block; they can be accessed only within that block and go out of existence when that block ends. Global variables have global scope and are available anywhere within your program. |
|
|
|
|
|
|
|
|
New Term: Variables defined outside of any function have global scope and thus are available from any function in the program, including main(). |
|
|
|
|
|
|
|
|
In C++, global variables are avoided because they can create very confusing code that is hard to maintain. You won't see global variables in any code in this book, nor in any programs I write. |
|
|
|
|
|
|
|
|
There is virtually no limit to the number or types of statements that can be in a function body. |
|
|
|
|
|