## Scope Rules

### Local Variables

Variables can be declared in function body(example from the last lecture)
 ```double finalScore(double midterm, double exam) { // result1 and result2 local variables double result1 = 0.5*midterm + 0.5*exam; double result2 = 0.4*midterm + 0.6*exam; // the final score is the max of result1 and result2 if(result1 >= result2) return result1; else return result2; } ```
• The variables declared within a function are said to be "local to the function". They are also called local variables
• Their scope(lifetime) is limited to the function
• `main` is a (special) function. All variables declared there are "local to main".
• Other parts of the program do not know local variables exist; you can repeat names
What happens if you have one variable in a function and one variable in `main` with the same name?
 ```#include using namespace std; void f(); void main() { int x; x = 5; f(); cout << "In main, x is " << x << ".\n"; char line[100]; cin.getline(line,100); } void f() { int x = 2; cout << "In f, x is " << x << ".\n"; } ```
Output
 ```In f, x is 2. In main, x is 5. ```
• Although there are 2 `x` in the program, the `x` in `main` is different from the `x` in the function `f`.
• variables in `f` (resp `main`) is local to `f` (resp `main`).
The scope of x in main
```#include<iostream>
using namespace std;
void f();
```
 ```void main() { int x; x = 5; f(); cout << "In main, x is " << x << ".\n"; char line[100]; cin.getline(line,100); } ```
```void f() {
int x = 2;
cout << "In f, x is " << x << ".\n";
}
```

The scope of x in f
```#include<iostream>
using namespace std;
void f();

void main() {
int x;
x = 5;
f();
cout << "In main, x is " << x << ".\n";

char line[100];
cin.getline(line,100);
}
```
 ```void f() { int x = 2; cout << "In f, x is " << x << ".\n"; } ```
User of functions doesn't have to worry about variable names he choose.

### Constants

 ```#include using namespace std; void main() { const double TAX_RATE = 0.25; double income; cout << "Please enter your income in year 2001: "; cin >> income; cout << "Your tax is " << income*TAX_RATE << ".\n"; char line[100]; cin.getline(line,100); cin.getline(line,100); } ```

Output
 ```Please enter your income in year 2001: 33259 Your tax is 8314.75. ```

Syntax
 `const` type variableName = value;
• initialize variables so that it cannot be changed
• If you programs try to change one of these variables, it produces a compiling error
• This make sure that you won't change constants accidentally
• Once a constant is declared, it can be used anywhere the number is allowed and it will have exactly the same meaning as the number it names.
• Most people use all uppercase letters for constants.
An example with error. You can't compile the program.
 ```#include using namespace std; void main() { const double TAX_RATE = 0.25; TAX_RATE = 0.33; // error!!!! You can't reassign value to a constant char line[100]; cin.getline(line,100); } ```

### Global constants and global variables

 ```#include #include using namespace std; const double PI = 3.14159; // returns the area of a circle with the specified radius double area(double radius); // returns the volume of a sphere with the specified radius double volume(double radius); void main() { double radius; cout << "Enter a radius (in inches): "; cin >> radius; cout << "Radius = " << radius << " inches\n" << "Area of circle = " << area(radius) << " square inches\n" << "Volume of sphere = " << volume(radius) << " cubic inches\n"; char line[100]; cin.getline(line,100); } double area(double radius) { return PI*pow(radius, 2); } double volume(double radius) { return 4.0/3*PI*pow(radius,3); } ```

Sample Output
 ```Enter a radius (in inches): 2 Radius = 2 inches Area of circle = 12.5664 square inches Volume of sphere = 33.5103 cubic inches ```
• variables declared inside a function body is a variable local to the function.
• variables declared outside of any function body (and outside the functin body of `main`) are called global variables
• Global variables can be used can be used anywhere in your program, i.e. the scope of global variables is the whole program.
• although you can place declarations of global variables anywhere outside the function bodies, it is better the place them before the `main`.

### Variables declared in a for loop

A variable can be declared in the heading of a `for` statements so that the variable is both declared and initialized at the start of the statments. example
```for(int n = 0; n <= 10; n++)
sum = sum + n;
```
The variable `n` is local to the body of loop.

### Call-by-value parameter

What is the output of the following program?
 ```#include using namespace std; void increase(int); void main() { int x = 1; cout << "Before calling the increase function, x is " << x << ".\n"; increase(x); cout << "After calling the increase function, x is " << x << ".\n"; char line[100]; cin.getline(line,100); } void increase(int x) { x++; } ```
Output
 ```Before calling the increase function, x is 1. After calling the increase function, x is 1. ```
The codes will be explained during the lectures.