Math2220 Lecture 9

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;
}
What happens if you have one variable in a function and one variable in main with the same name?
#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";
}
Output
In f, x is 2.
In main, x is 5.
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";
}
What is the advantage?
User of functions doesn't have to worry about variable names he choose.

Constants

#include<iostream>
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;
An example with error. You can't compile the program.
#include<iostream>
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<iostream>
#include<cmath>
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 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<iostream>
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.