Global & Local Identifiers

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Chapter 8 Technicalities: Functions, etc. Bjarne Stroustrup
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.
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 11 – Fundraiser Application: Introducing Scope.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 5 Functions.
Functions Most useful programs are much larger than the programs that we have considered so far. To make large programs manageable, programmers modularize.
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
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 Modularity In “C”. 2 General Syntax data_type function_Name (parameter list) { … return expression; // return output } Body of the function is a compound.
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.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
C++ Functions. 2 Agenda What is a function? What is a function? Types of C++ functions: Types of C++ functions: Standard functions Standard functions.
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).
1 Chapter 9 Scope, Lifetime, and More on Functions.
Beginning C++ Through Game Programming, Second Edition by Michael Dawson.
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 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
Functions Modules in C++ are called functions and classes Functions are block of code separated from main() which do a certain task every C++ program must.
1 FUNCTIONS - I Chapter 5. 2 What are functions ? Large programs can be modularized into sub programs which are smaller, accomplish a specific task and.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
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.
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.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
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 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
Functions, Scope, and The Free Store Functions Functions must be declared by a function prototype before they are invoked, return_type Function_name(type,
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
FUNCTIONS - What Is A Function? - Advantages Function Declaration
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.
Functions in C++ Top Down Design with Functions. Top-down Design Big picture first broken down into smaller pieces.
Lesson xx Why use functions Program that needs a function Function header Function body Program rewritten using a function.
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.
What Is? function predefined, programmer-defined
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Predefined Functions Revisited
Bill Tucker Austin Community College COSC 1315
A Lecture for the c++ Course
FUNCTIONS IN C++.
Lecture 4-7 Classes and Objects
User Defined Functions
Chapter 5 Function Basics
Pointers, Dynamic Data, and Reference Types
Screen output // Definition and use of variables
Functions.
Anatomy of a Function Part 1
Object Oriented Programming Using C++
Writing Functions.
Namespaces How Shall I Name Thee?.
Dynamic Memory.
Predefined Functions Revisited
Functions Imran Rashid CTO at ManiWeber Technologies.
CMSC 202 Lesson 6 Functions II.
What Is? function predefined, programmer-defined
CS1201: Programming Language 2
Pointers, Dynamic Data, and Reference Types
Scope Rules.
Presentation transcript:

Global & Local Identifiers Scope Global & Local Identifiers

Scope Definition: the portions of a program where an identifier “is defined” (may be used).

Local Variables Definition: a variable declared inside a Block. Scope: from the Declaration statement to the end of the Block

Local Variables Example 1: void fun() { float f; int a; //scope of a begins ... } // scope of a ends void main() { a++; // syntax error: a undef. }

Local Variables Example 2: void fun() { for (int i = 0; i < 3; i++) { cout << i; } // scope of i ends here cout << i; // syntax error: i undef }

Local Variables Example 3: void fun(int x) { cout << i; // syntax error: i undef if (x < 0) { int i=x; // scope of i begins cout << i; // OK ... } // scope of i ends }

Formal Arguments are Local // scope of arguments is entire function void fun(int x) // scope of x begins { ... } // scope of x ends void main() { x++; // syntax error: x undef. }

Global Variables Definition: a variable declared outside any Block. Scope: from the Declaration statement to the end of the source file

Global Variables Allocation: before main() begins Deallocation: after main() ends

Global Variables using namespace std; int x = 3; // scope of x begins void fun() { ... x--; // OK } void main() { x++; // OK // scope of x ends

Use of Global Variables Structured Programming! but... Use of Global Variables is a Major Violation of Structured Programming! (almost as bad as TWTSNBU) [The Word That Shall Not Be Uttered]

Global Variables Why are Global Variables a Major Violation of SP? 1. Inhibits Reuse of Functions int x; // global ... // 300 lines of code here // So Great! copy/paste/use in ANY program! void geniusFunction() { ... x = x * 2; // don’t forget to copy/paste ... // global declaration of x! }

Global Variables Why are Global Variables a Major Violation of SP? 2. Side Effect: value of a global variable changes for no apparent reason int x; // global void fun() { ... x++; ... } void main() { x = 0; fun(); cout << x; // prints 1 } // where did x change??

Global Constants Definition: a constant declared outside any block. Scope: same as Global Variables Allocation: same as Global Variables Deallocation: same as Global Variables

Global Constants but... They are NOT considered a Major Violation of Structured Programming

Global Constants Why are they not a Major Violation of SP? 1. No Side Effects since constants don’t change value 2. Promote Consistency value is the same for ALL functions But: They DO inhibit reuse, but techniques can be used to limit problems Therefore: Advantages outweigh Disadvantages

Global Constants // same value of pi for all functions const double pi = 3.1415926; double circleArea(double r) { return pi * r * r; } double circumference(double r) { ... } double sphereVolume(double r) { ... } double sphereSurface(double r) { ... }

Masking Definition: when a local identifier has the same name as a global (oll) identifier, blocking access to the global (oll) identifier. oll: “or less-local”

Masking Also called Hole In Scope

Masking int x = 0; // global x declared void fun1 () { x = 5; // changes global x } void fun2() { float x; // local x declared x = 3; // changes local x, } // not the global x void main() { x = 4; // changes global x

Masking void main() { int x = 0; // local x if (x <= 0) { float x; // more-local x x = 4; cout << x; // prints 4 } cout << x; // prints 0

Global Functions Definition: a function declared outside any other any block. Scope: where the function may be invoked. - begins where the function is Declared or Prototyped - ends at the end of the source file.

Local Functions Definition: a function declared inside the declaration of another function. Scope: inside the “outer” function only Not allowed by most C++ compilers but available in other languages

Local Functions void globalFun() { void localFun() { // C++ syntax error ... // just cut/paste } // outside/before ... // globalFun(){ } localFun(); } // end globalFun()

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; //note: y & z out of scope! } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl;

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; } program ending...

Semantics Example int g = 3; void fun(int a, int & b) { int c = a + b; a++; b--; g *=2; cout << a << " " << b << " " << c << " " << g << endl; } void main() { int y=2, z=10; fun(y,z); cout << y << " " << z << " " << g << endl; } program ended

Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x; }

Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x; }

Semantics: Masking void main() { int x = 0; if (x <= 0) { True! float x; x = 4; cout << x; }

Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x; }

Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; Changes the Most-Local cout << x; }

Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x << ” ”; } cout << x;

Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x << ” ”; } locals declared in this block are deallocated cout << x; }

Semantics: Masking void main() { int x = 0; if (x <= 0) { float x; x = 4; cout << x << ” ”; } cout << x;

Vocabulary Scope Global Local Side Effect Masking Hole In Scope Term Definition Scope the portions of a program where an Identifier is defined Global an identifier declared outside of any function. Its scope is from its declaration through the end of the source file. Local an identifier declared inside a function. Its scope is from its declaration through the ending brace of the block in which it is declared. Side Effect when an invoked function changes the value of a global variable, the change is not obvious in the invoking function. Masking when a local identifier has the same name as a global identifier, blocking access to the global identifier. Hole In Scope synonym for “Masking”