Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE202: Lecture 13The Ohio State University1 Function Scope.

Similar presentations


Presentation on theme: "CSE202: Lecture 13The Ohio State University1 Function Scope."— Presentation transcript:

1 CSE202: Lecture 13The Ohio State University1 Function Scope

2 CSE202: Lecture 13The Ohio State University2 Scope From wikipedia.org: –Scope: “the visibility or accessibility of variables from different parts of the program.”

3 CSE202: Lecture 13The Ohio State University3 circleArea3.cpp()... double computeCircleArea(double radius); int main() { double r(3.0); // radius of circle double a(0.0); // area of circle a = computeCircleArea(r); // call function cout << "Radius: " << r << " Area: " << a << endl;... } double computeCircleArea(double radius) { double area = 0.0; // area of circle area = M_PI * radius * radius; return(area); } Scope of “r” and “a” is function main(). Scope of “radius” and “area” is function computeCircleArea().

4 CSE202: Lecture 13The Ohio State University4 scopeError.cpp()... double computeCircleArea(double radius); int main() { double r(3.0); // radius of circle double a(0.0); // area of circle a = computeCircleArea(r); // call function cout << "Radius: " << r << " Area: " << a << endl;... } double computeCircleArea(double radius) { double area = 0.0; // area of circle area = M_PI * r * r; // *** ERROR: Not in scope return(area); } Scope of “r” and “a” is function main(). Scope of “radius” and “area” is function computeCircleArea().

5 CSE202: Lecture 13The Ohio State University5 > g++ scopeError.exe scopeError.cpp: In function ‘double computeCircleArea(double)’: scopeError.cpp:26: error: ‘r’ was not declared in this scope 10.int main() 11.{ 12. double r(3.0); // radius of circle... 19.}... 22.double computeCircleArea(double radius) 23.{ 24. double area = 0.0; // area of circle 25. 26. area = M_PI * r * r; // *** ERROR: Not in scope 27. return(area); 28.}

6 CSE202: Lecture 13The Ohio State University6 scopeError2.cpp()... double computeCircleArea(double radius); int main() { double r(3.0); // radius of circle double a(0.0); // area of circle a = computeCircleArea(r); // call function cout << "Area = " << area << endl; // *** ERROR... } double computeCircleArea(double radius) { double area = 0.0; // area of circle area = M_PI * r * r; return(area); } Scope of “r” and “a” is function main(). Scope of “radius” and “area” is function computeCircleArea().

7 CSE202: Lecture 13The Ohio State University7 > g++ scopeError2.exe scopeError2.cpp: In function ‘int main()’: scopeError2.cpp:16: error: ‘area’ was not declared in this scope 10.int main() 11.{... 15. a = computeCircleArea(r); // call function 16. cout << "Area = " << area << endl; // *** ERROR... 19.}... 22.double computeCircleArea(double radius) 23.{ 24. double area = 0.0; // area of circle 25. 26. area = M_PI * radius * radius; 27. return(area); 28.}

8 CSE202: Lecture 13The Ohio State University8 scopeExample.cpp()... void f(int x); void g(int y); int main() { int a(5); f(5); g(5); cout << "main: a = " << a << endl; return 0; } void f(int x) { int a(0); a = 2*x; cout << "f: a = " << a << endl; return; } void g(int x) { int a(0); a = x*x; cout << "g: a = " << a << endl; return; }

9 CSE202: Lecture 13The Ohio State University9... void f(int x); void g(int y); int main() { int a(5); f(5); g(5); cout << "main: a = " << a << endl;... void f(int x) { int a(0); a = 2*x; cout << "f: a = " << a << endl;... void g(int x) { int a(0); a = x*x; cout << "g: a = " << a << endl;... > scopeExample.exe f: a = 10 g: a = 25 main: a = 5

10 CSE202: Lecture 13The Ohio State University10 Global Variables

11 CSE202: Lecture 13The Ohio State University11 Global Variables A global variable is a variable whose scope is the entire program.

12 CSE202: Lecture 13The Ohio State University12 globalExample.cpp()... void read_inputs(); // global variables int age(0); double height(0.0), weight(0.0); int main() { read_inputs(); cout << "age = " << age << endl; cout << "height = " << height << endl; cout << "weight = " << weight << endl; return 0; } void read_inputs() { cout << "Enter age: "; cin >> age; cout << "Enter height: "; cin >> height; cout << "Enter weight: "; cin >> weight; }

13 CSE202: Lecture 13The Ohio State University13... // global variables int age(0); double height(0.0), weight(0.0); int main() { read_inputs(); cout << "age = " << age << endl; cout << "height = " << height << endl; cout << "weight = " << weight << endl;... void read_inputs() { cout << "Enter age: "; cin >> age; cout << "Enter height: "; cin >> height; cout << "Enter weight: "; cin >> weight; } > globalExample.exe Enter age: 35 Enter height: 5.8 Enter weight: 155 age = 35 height = 5.8 weight = 155

14 CSE202: Lecture 13The Ohio State University14 globalExample2.cpp()... // global variables const double TOLERANCE = 0.001; int main() { double x(0.0), y(0.0); cout << "Enter x, y: "; cin >> x >> y; cout << "1/x = " << reciprocal(x) << endl; if (equals(x,y)) { cout << "x equals y." << endl; } else { cout << "x does not equal y." << endl; } return 0; } double reciprocal(double x) { if (abs(x) >= TOLERANCE) { return(1/x); } else { return(1/TOLERANCE); } } bool equals(double x, double y) { if (abs(x-y) < TOLERANCE) { return(true); } else { return(false); } }

15 CSE202: Lecture 13The Ohio State University15... // global variables const double TOLERANCE = 0.001;... int main() {... cout << "1/x = " << reciprocal(x) << endl; if (equals(x,y)) { cout << "x equals y." << endl; } else { cout << "x does not equal y." << endl; } return 0; } double reciprocal(double x) { if (abs(x) >= TOLERANCE) { return(1/x); } else { return(1/TOLERANCE); } } bool equals(double x, double y) { if (abs(x-y) < TOLERANCE) { return(true); } else { return(false); } } > globalExample2.exe Enter x, y: 0.00004 0.00002 1/x = 1000 x equals y.

16 CSE202: Lecture 13The Ohio State University16 globalError.cpp()... void read_inputs(); // global variables int age(0); double height(0.0), weight(0.0); int main() { read_inputs(); cout << "age = " << age << endl; cout << "height = " << height << endl; cout << "weight = " << weight << endl; return 0; } void read_inputs() { // *** ERROR. Masks global variables. int age(0); double height(0.0), weight(0.0); cout << "Enter age: "; cin >> age; cout << "Enter height: "; cin >> height; cout << "Enter weight: "; cin >> weight; }

17 CSE202: Lecture 13The Ohio State University17... // global variables int age(0); double height(0.0), weight(0.0); int main() { read_inputs(); cout << "age = " << age << endl; cout << "height = " << height << endl; cout << "weight = " << weight << endl;... void read_inputs() { // *** ERROR. Masks global variables. int age(0); double height(0.0), weight(0.0); cout << "Enter age: "; cin >> age; cout << "Enter height: "; cin >> height; cout << "Enter weight: "; cin >> weight; } > globalExample.exe Enter age: 35 Enter height: 5.8 Enter weight: 155 age = 0 height = 0 weight = 0 WHY?

18 CSE202: Lecture 13The Ohio State University18 A Useful Function…

19 CSE202: Lecture 13The Ohio State University19 Pseudorandom Numbers (1) It is often useful to have your program generate a random number. Can you think of a use? C++ has a function called rand() that returns a “random” number when called. It is not really random at all, but repeated calls of the rand() function produces a sequence of numbers that satisfy some properties of random numbers. To use rand() you must: #include

20 CSE202: Lecture 13The Ohio State University20 random1.cpp() #include #include // contains rand() using namespace std; int main() { for (int i = 0; i < 10; i++) { // output a random integer between 0 and RAND_MAX cout << rand() << endl; }; return 0; }

21 CSE202: Lecture 13The Ohio State University21 Pseudorandom Numbers (2) rand() will always produce the same series of random numbers each time a program is run. This is not very random or desirable. Luckily there is a function called srand() that allows you to provide a starting value (called a “seed”) for rand()

22 CSE202: Lecture 13The Ohio State University22 random2.cpp() #include // contains rand()... int main() { unsigned int seed; cout << "Enter seed: "; cin >> seed; srand(seed); // set the random seed for (int i = 0; i < 10; i++) { // output a random integer between 0 and RAND_MAX cout << rand() << endl; };.

23 CSE202: Lecture 13The Ohio State University23 Pseudorandom Numbers (3) To automatically set the seed, use the computer’s internal clock. #include time(NULL); // return the time // (in seconds since Jan. 1, 1970)

24 CSE202: Lecture 13The Ohio State University24 random3.cpp() #include // contains rand() and srand() #include // contains time()... int main() { srand(time(NULL)); // set the random seed for (int i = 0; i < 10; i++) { // output a random integer between 0 and RAND_MAX cout << rand() << endl; // No need to reseed. An internal seed changes at every call to rand(). }; return 0; }

25 CSE202: Lecture 13The Ohio State University25 random4.cpp()... unsigned int seed = time(NULL); // set seed using time() srand(seed); // set the random seed for (int i = 0; i < 10; i++) { cout << rand() << " "; // output a random integer }; cout << endl; srand(seed); // reset the random seed to seed for (int i = 0; i < 10; i++) { cout << rand() << " "; // output a random integer }; cout << endl;...

26 CSE202: Lecture 13The Ohio State University26 Pseudorandom Numbers (4) Always test and debug your software using a fixed random seed so you can replicate your programs behaviour. cout << "Enter seed: "; cin >> seed; srand(seed); // set the random seed

27 CSE202: Lecture 13The Ohio State University27 random5.cpp()... int main() { srand(time(NULL)); // set the random seed for (int i = 0; i < 10; i++) { // output a random integer between 0 and 9, inclusive; unsigned int r = rand()%10; cout << r << " "; }; cout << endl; return 0; }

28 CSE202: Lecture 13The Ohio State University28 random6.cpp()... int main() { srand(time(NULL)); // set the random seed for (int i = 0; i < 10; i++) { // output a random integer between 1 and 10, inclusive; unsigned int r = rand()%10 + 1; cout << r << " "; }; cout << endl; return 0; }

29 CSE202: Lecture 13The Ohio State University29 Generating Random Integers How can we generate a random integer between 100 and 120, inclusive? How can we generate a random integer between -5 and 5, inclusive? How can we generate a random number between 0.0 and 1.0, inclusive?

30 CSE202: Lecture 13The Ohio State University30 randomFloat.cpp()... int main() { srand(time(NULL)); // set the random seed for (int i = 0; i < 10; i++) { // output a random number between 0 and 1, inclusive double r = double(rand())/RAND_MAX; cout << r << endl; } return 0; }

31 CSE202: Lecture 13The Ohio State University31 Generating Random Rational Numbers How can we generate a random number between 0.0 and 2.0, inclusive? How can we generate a random number between -1.0 and 1.0, inclusive?

32 CSE202: Lecture 13The Ohio State University32 coinToss.cpp()... int choice; char c; srand(time(NULL)); // set the random seed while (1) // run forever { cout << "Heads or Tails? "; cin >> c; if (c == 'h' || c == 'H') { choice = 1; } else { choice = 0; }; int r = rand()%2; // generate a random 0 or 1 if (r == choice) { cout << "You win." << endl << endl; } else { cout << "Computer wins." << endl << endl; } }...


Download ppt "CSE202: Lecture 13The Ohio State University1 Function Scope."

Similar presentations


Ads by Google