Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.

Slides:



Advertisements
Similar presentations
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Advertisements

Chapter 5 Functions.
C++ Basics Prof. Shermane Austin. Learning Programming Language Basics Data Types – simple Expressions Relational and Logical Operators Conditional Statements.
Computer Science 1620 Loops.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
Computer Science 1620 Function Scope & Global Variables.
Methods of variable creation Global variable –declared very early stage –available at all times from anywhere –created at the start of the program, and.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
Chapter 6: Functions.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
1 Chapter 9 Scope, Lifetime, and More on Functions.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Value Returning Functions // Function prototype int Largest(int num1, int num2, int num3); Function Name Type Parameters Type of parameters Formal parameters.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
#include using namespace std; // Declare a function. void check(int, double, double); int main() { check(1, 2.3, 4.56); check(7, 8.9, 10.11); } void check(int.
Quiz 2 Results. What Is Wrong? #include using namespace std int Main() { // Say Hello 4 times for(i == 0; i < 3; i++) { cout >> "Hello World!"
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
Modular Programming – User Defined Functions. CSCE 1062 Outline  Modular programming – user defined functions  Value returning functions  return statement.
1 MORE ON MODULAR DESIGN: MODULE COMMUNICATIONS. 2 WHEN A FUNCTION IS INVOKED, MEMORY IS ALLOCATED LOCALLY FOR THE FORMAL PARAMETERS AND THE VALUE OF.
1 Chapter 9 Scope, Lifetime, and More on Functions.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 8/30/06CS150 Introduction to Computer Science 1 Your First C++ Program.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Variable Scope. When you declare a variable, that name and value is only “alive” for some parts of the program  We must declare variables before we use.
Advanced Programming in C
Review 1.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Branching Constructs Review
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 4-7 Classes and Objects
Chapter 9 Scope, Lifetime, and More on Functions
Counting Loops.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Alternate Version of STARTING OUT WITH C++ 4th Edition
CS150 Introduction to Computer Science 1
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
FOR statement a compact notation for a WHILE e.g. sumgrades = 0;
Programming Fundamental
Presentation transcript:

Computer Science 1620 Lifetime & Scope

Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs at declaration Variable destruction: memory is returned to the program (for use with another variable) occurs at ….

Variable Destruction a local variable (the kind we are used to) lives until the end of its statement block this may be the entire function this could also be a compound statement when a variable is destroyed its memory is no longer reserved – it may be used by another variable the variable can no longer be used

Variable Lifetime - Example #include using namespace std; int main() { int x; x = 2; return 0; } Variable x's lifetime End of statement block that x was declared in.

Variable Lifetime - Example #include using namespace std; int main() { int num; cout << "Please enter a number" << endl; cin >> num; if (num < 0) { int neg = -num; cout << "|" << num << "| = " << neg << endl; } return 0; } Variable num's lifetime End of statement block that num was declared in.

Variable Lifetime - Example #include using namespace std; int main() { int num; cout << "Please enter a number" << endl; cin >> num; if (num < 0) { int neg = -num; cout << "|" << num << "| = " << neg << endl; } return 0; } Variable neg's lifetime End of statement block that neg was declared in.

Variable Lifetime – Loops if a variable is declared inside the loop statement, then its lifetime lasts until the end of that loop statement this means that the variable is (theoretically) created over and over again if a variable is declared in the initialization of a for loop, then the variable's lifetime lasts until the end of the loop this means that the variable is not redeclared over and over

Variable Lifetime - Loops #include using namespace std; int main() { for (int a = 0; a < 10; a++) { int b = a * a; cout << b << endl; } return 0; } a lasts from loop initialization until the loop has finished executing. b lasts from declaration until the loop statement has been executed.

Variable Scope the area of a program where a variable can be used the scope of a variable is anywhere in the program where: the variable is alive (between its creation and destruction) the variable is not being hidden by another variable of the same name if you try to use a variable outside of its scope, a compiler error results

Variable Scope - Example #include using namespace std; int main() { int x; x = 4; return 0; } Variable x's lifetime This code is fine … variable x is being used within its lifetime.

Variable Scope - Example #include using namespace std; int main() { x = 4; int x; return 0; } Variable x's lifetime This code generates a compiler error, since we are trying to use x outside of its scope.

Variable Scope - Example #include using namespace std; int main() { if (3 < 4) { int x = 5; } cout << "x = " << x << endl; return 0; } Variable x's lifetime This code generates a compiler error, since we are trying to use x outside of its scope.

Variable Scope - Example #include using namespace std; int main() { int x = 0; if (3 < 4) { x = 5; } cout << "x = " << x << endl; return 0; } Variable x's lifetime This code is fine … x is being used within its lifetime.

Variable Scope - Example #include using namespace std; int main() { if (3 < 4) { int x = 5; cout << "x = " << x << endl; } else { x = 2; cout << "x = " << x << endl; } return 0; } Variable x's lifetime This code generates a compiler error, since we are trying to use x outside of its scope.

Variable Scope - Example #include using namespace std; int main() { for (int x = 0; x < 10; x++) { cout << "x = " << x << endl; } return 0; } Variable x's lifetime This code is fine … we are using x within its lifetime.

Variable Scope - Example #include using namespace std; int main() { for (int x = 0; x < 10; x++) { cout << "x = " << x << endl; } cout << "x = " << x << endl; return 0; } Variable x's lifetime This code generates a compiler error, since we are trying to use x outside of its scope.

Variable Scope - Example #include using namespace std; int main() { for (int x = 0; x < 10; x++, y++) { int y = 2 * x; cout << "y = " << y << endl; } return 0; } Variable y's lifetime This code generates a compiler error, since we are trying to use y outside of its scope.

Nested Code Blocks we have seen code blocks nested inside other code blocks e.g. if statements int main() { int x, y; cout << "Please enter two integers: "; cin >> x >> y; if (x < y) { if (y == 10) { cout << "x must be less than 10" << endl; } y = 0; } return 0; }

Nested Code Blocks we have seen code blocks nested inside other code blocks e.g. loop statements int main() { int x, y; cout << "Please enter two integers: "; cin >> x >> y; while (x < y) { while (y < 10) { y++; } x++; } return 0; }

Nested Code Blocks we have seen code blocks nested inside other code blocks standalone compound statements int main() { int x, y; cout << "Please enter two integers: "; cin >> x >> y; { y++; } x++; } return 0; }

Variable Declaration until now, we have said that you cannot declare two variables of the same name in the same function this is actually stronger than the actual rule you cannot declare two variables of the same name in the same statement block

Variable Declaration - Example #include using namespace std; int main() { int a = 10; cout << a << endl; int a = 9; cout << a << endl; return 0; } This code generates a compiler error, since we are trying to declare two variables with the name a inside the same statement block.

Variable Declaration two variables can have the same name if they are declared in different statement blocks #include using namespace std; int main() { int a = 10; cout << a << endl; { int a = 9; cout << a << endl; } return 0; }

Variable Declaration two variables can have the same name if they are declared in different statement blocks #include using namespace std; int main() { int a = 10; cout << a << endl; if (3 < 4) { int a = 9; cout << a << endl; } return 0; }

Variable Lifetime two variables can have the same name if they are declared in different statement blocks #include using namespace std; int main() { int a = 10; cout << a << endl; if (3 < 4) { int a = 9; cout << a << endl; } return 0; } First variable a's lifetime Second variable a's lifetime Overlap

Variable Lifetime two variable's with the same name may have overlapping lifetimes one of the variables is declared in a nested statement block Question: if the variable name is used inside the overlap area, which variable is being referred to?

Variable Lifetime two variables can have the same name if they are declared in different statement blocks #include using namespace std; int main() { int a = 10; cout << a << endl; if (3 < 4) { int a = 9; cout << a << endl; } return 0; } Does this refer to the first or the second a?

Rule: When two variables have overlapping lifetimes, the variable declared in the nested statement block hides the variable declared in the outer statement block When a local variable is hidden, it is out of scope it cannot be used* The outer variable remains hidden until the lifetime of the inner variable terminates * this is not true for global variables, or those declared in a namespace

Variable Lifetime two variables can have the same name if they are declared in different statement blocks #include using namespace std; int main() { int a = 10; cout << a << endl; if (3 < 4) { int a = 9; cout << a << endl; } return 0; } The declaration of this variable hides the outside variable.

Example: What is the output of the following code? #include using namespace std; int main() { int a = 25; int b = 17; cout << " a = " << a << " b = " << b << endl; { float a = 46.25; int c = 10; cout << " a = " << a << " b = " << b << " c = " << c << endl; } cout << " a = " << a << " b = " << b << endl; return 0; }

We will have much more to say about scope when we talk about functions global variables

... Next: short guidelines for formatting source code.

Code format guidelines Comments: each file (purpose, author, date) complicated code some variable declarations

Code format guidelines // // A silly program to play with scope // Author: Terrence Hill // Date: Oct. 13, 1964 // #include using namespace std; int main() { for (int i=0; i<5; i++) { int a; if (i%2 == 0) { int a; a = i; } cout << a << endl; } file comment

Code format guidelines #include using namespace std; int main() { int num = 15; int first = 0; int second = 1; int total = 0; for (int i = 1; i < num; i++) { total = first + second; first = second; second = total; } cout << total << endl; return 0; } Not clear what this does.

Code format guidelines #include using namespace std; int main() { // computing 15 Fibonacci numbers int num = 15; int first = 0; int second = 1; int total = 0; for (int i = 1; i < num; i++) { total = first + second; first = second; second = total; } cout << total << endl; return 0; }

One statement/declaration per line exceptions: many short assignments a = 2; b = 3; c = 4; d = 5; // etc...

Empty row between declarations and code exceptions: short compound blocks

// // A silly program to play with scope // Author: Terrence Hill // Date: Oct. 13, 1964 // #include using namespace std; int main() { for (int i=0; i<5; i++) { int a; if (i%2 == 0) { int a; a = i; } cout << a << endl; } separate declarations from code short blocks (exceptions)

Separate compound statements from rest Nested statements should be indented

// // A silly program to play with scope // Author: Terrence Hill // Date: Oct. 13, 1964 // #include using namespace std; int main() { for (int i=0; i<5; i++) { int a; if (i%2 == 0) { int a; a = i; } cout << a << endl; } separate compound statements indent