Chapter 8 Scope of variables Name reuse. Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends.

Slides:



Advertisements
Similar presentations
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
Advertisements

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.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Chapter 5 Functions.
J. P. Cohoon and J. W. Davidson © 1999 McGraw-Hill, Inc. Programmer-defined functions Development of simple functions using value parameters.
File Review Declare the File Stream Object Name –ofstream for output to file –ifstream for input from file Associate a File Name with the File Stream Object.
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.
Introduction to Programming with Java, for Beginners Scope.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
1 Chapter 9 Scope, Lifetime, and More on Functions.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
CSCI 130 Scope of Variables Chapter 6. What is scope Parts of program which can access a variable –accessibility –visibility How long variable takes up.
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
1 CS 192 Lecture 5 Winter 2003 December 10-11, 2003 Dr. Shafay Shamail.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
ITEC 320 C++ Examples.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Chapter 4 Selection: the if-else and switch-case instructions.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Chapter 3 Functions. 2 Overview u 3.2 Using C++ functions  Passing arguments  Header files & libraries u Writing C++ functions  Prototype  Definition.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
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 User defined function is a code segment (block) that perform an specific action. Function Definition: Function Definition: Return_DT F_name (
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
1 Scope Lifetime Functions (the Sequel) Chapter 8.
Lecturer: Nguyen Thi Hien Software Engineering Department Home page: hienngong.wordpress.com Chapter 2: Language C++
C++ Namespaces, Exceptions CSci 588: Data Structures, Algorithms and Software Design All material not from online sources copyright © Travis Desell, 2011.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Chapter 2 Creating a C++ Program. Elements of a C++ Program Four basic ways of structuring a program Four basic ways of structuring a program 1.Sequencing.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
Computer Programming II Lecture 4. Functions - In C++ we use modules to divide the program into smaller and manageable code. These modules are called.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 Chapter 8 Scope, Lifetime, and More on Functions CS185/09 - Introduction to Programming Caldwell College.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
1 Sections 6.4 – 6.5 Methods and Variables Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Test 2 Review Outline.
Suppose we want to print out the word MISSISSIPPI in big letters.
Functions and Structured Programming
Global & Local Identifiers
User Defined Functions
Chapter 5 Function Basics
Chapter 7 Conditional Statements
Introduction to Programming
Namespaces How Shall I Name Thee?.
The Function Prototype
COMS 261 Computer Science I
Predefined Functions Revisited
Scope of Identifier The Scope of an identifier (or named constant) means the region of program where it is legal to use that.
Variables and Constants
Scope Rules.
Presentation transcript:

Chapter 8 Scope of variables Name reuse

Scope The region of program code where it is legal to reference (use) a variable The scope of a variable depends primarily on where you declare it There are also keywords you can use when declaring a variable that affect its scope

Variable Scope Global – can be referenced within any function in the program Function – can be referenced anywhere within a function Block – can be referenced within a block (curly braces {})

Global Scope Variables defined outside of functions are global A global variable can be used by any function in the file that is defined after the global variable It is best to avoid programmer-defined global variables Exceptions tend to be important constants Globals with appropriate declarations can even be used in other program files

Global Example const float pi = ; // usual use of global int i = 2; // example of global scope int main() { int inMain = 3; cout << i << endl; // print 2 cout << pi << endl; // print funca(inMain); return 0; } void funca(int passed) { cout << passed * i; // print 6 cout << pi * i; // print }

Function Scope Parameters passed to a function and any variables declared at the start of the function have function scope Function scope means that they can be referenced anywhere in the function (including all blocks within the function)

Function Scope Example int main(int argc, char *argv[]){ int inMain = 3; // Function scope cout << inMain << endl; // print 3 funca(inMain); cout << inFunca; // illegal return 0; } void funca(int passedParm) { // func scope int inFunca = 4; // Function scope cout << inFunca << endl; // Prints 4 cout << inMain << endl; // illegal cout << passedParm << endl; //prints 3 }

Blocks can be put anywhere a statement can be put Blocks within blocks are nested blocks Typical use of blocks are for while, for, if clauses A variable is known only within the block in which it is defined and in nested blocks of that block Block Scope A block is a list of statements within curly braces

Block Scope Example void funca(int passedParm) { function scope int inFunca = 4; // Function scope if (passedParm == 3) { int inIf = 5; // Block scope cout << inFunca <<endl; // Print 4 cout << inIf <<endl; // Print 5 } else { cout << inFunca <<endl;// Print 4 cout<< inIf; // illegal } cout << inIf; //illegal }

Name Reuse If a nested block defines a variable with the same name as enclosing block, the new definition is in effect in the nested block Each scope area can have a variable of the same name: Global (one variable only) Function (each function) Block (each block)

Name Reuse Example string s = "global"; // global i known everywhere int main () { string sreturn; string s = "main"; // this s is known within main cout << "s in main = " << s << endl; sreturn = func1(s); cout << "func1 returned " << sreturn << endl; sreturn = func2(s); cout << "s from func2 in main = " << sreturn << endl; return 0; }

string func1 (string local) { cout << "func1 got " << local << " from caller" << endl; cout << "s in func1 (gets global s) = " << s << endl; string s = "func1"; // now declare our own s s = func2(s); cout << "s from func2 in func1 = " << s << endl; return s; }

string func2 (string local) { if (local == "main") { string s = "if true"; return (s); } else { string s = "if false"; return (s); }

Using a File in a Function The file identifier (handle) is used by the operating system to keep track of the file (for example, what to read next) If you want to use a file in a function: Pass the file ID by reference, or Declare the file ID as a global (above main)

File Use in a Function Pass the file ID by reference: void readFile(int& custID, ifstream& myFile) { myFile >> custID; return; } OR make myFile a global variable