CSE202: Lecture 13The Ohio State University1 Function Scope.

Slides:



Advertisements
Similar presentations
CSE202: Lecture 19The Ohio State University1 C++ Classes.
Advertisements

Programmer-defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc.
1 Random number generation Using srand(), rand() & time(0) Searching and Sorting Demo Making searching & sorting more generic Overloading the functions.
Programming Functions: Passing Parameters by Reference.
The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp.
 Monday, 10/28/02, Slide #1 CS106 Introduction to CS1 Monday, 10/28/02  QUESTIONS on HW 03??  Today: Generating random numbers  Reading & exercises:
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
Computer Science 1620 Function Scope & Global Variables.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
C Lecture Notes Functions (Cont...). C Lecture Notes 5.8Calling Functions: Call by Value and Call by Reference Used when invoking functions Call by value.
CSE202: Lecture 10AThe Ohio State University1 Numerical Error.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Craps!. Example: A Game of Chance Craps simulator Rules – Roll two dice 7 or 11 on first throw, player wins 2, 3, or 12 on first throw, player loses 4,
1 Lecture 3 Part 1 Functions with math and randomness.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
Scope Accessibility of Names. Review We’ve seen that C++ permits a programmer to declare names and then use those names in a manner consistent with their.
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Introduction to C++ // Program description #include directives int main() { constant declarations variable declarations executable statements return.
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
CS1010E Programming Methodology Tutorial 4 Modular Programming with Functions C14,A15,D11,C08,C11,A02.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
CMSC 1041 Functions II Functions that return a value.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
CSIS 113A Lecture 5 Random Numbers, while, do-while.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
C++ Programming Lecture 10 Functions – Part II
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
Introduction to Programming Lecture 11. ARRAYS They are special kind of data type They are special kind of data type They are like data structures in.
Introduction to C Programming Lecture 6. Functions – Call by value – Call by reference Arrays Today's Lecture Includes.
Modular Programming ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
CSE202: Lecture 11The Ohio State University1 Functions.
Input a number #include using namespace std; int main() { int num; cout num; return 0; }
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
L what are predefined functions? l what is? n function name n argument(s) n return value n function call n function invocation n nested function call l.
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
UNIT 11 Random Numbers.
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
Lecture 5 Computer programming -1-. Input \ Output statement 1- Input (cin) : Use to input data from keyboard. Example : cin >> age; 2- Output (cout):
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 4 Functions.
Function Call Stack and Activation Frame Stack Just like a pile of dishes Support Two operations push() pop() LIFO (Last-In, First-Out) data structure.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Functions and Libraries. Reference parameters void setToZero(int var) { var = 0; } int main() { int var = 0; setToZero(var); cout
1 Generating Random Numbers Textbook ch.6, pg
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Static Variables. Function Scope  “Normal” local variables go out of scope and are deallocated when a function terminates.
The Ohio State University
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
Controlling execution - iteration
The Ohio State University
C++ Arrays.
توابع در C++ قسمت اول اصول كامپيوتر 1.
Chapter 2 Elementary Programming
Random Number Generation
Screen output // Definition and use of variables
CS150 Introduction to Computer Science 1
Fundamental Programming
Predefined Functions Revisited
CS 144 Advanced C++ Programming January 31 Class Meeting
CS150 Introduction to Computer Science 1
Presentation transcript:

CSE202: Lecture 13The Ohio State University1 Function Scope

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

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().

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().

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 } double computeCircleArea(double radius) 23.{ 24. double area = 0.0; // area of circle area = M_PI * r * r; // *** ERROR: Not in scope 27. return(area); 28.}

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().

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.{ a = computeCircleArea(r); // call function 16. cout << "Area = " << area << endl; // *** ERROR } double computeCircleArea(double radius) 23.{ 24. double area = 0.0; // area of circle area = M_PI * radius * radius; 27. return(area); 28.}

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; }

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

CSE202: Lecture 13The Ohio State University10 Global Variables

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

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; }

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

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); } }

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: /x = 1000 x equals y.

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; }

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?

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

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

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; }

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()

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; };.

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)

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; }

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;...

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

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; }

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; }

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?

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; }

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?

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; } }...