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.

Slides:



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

Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
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.
Programming Scope of Identifiers. COMP102 Prog. Fundamentals I: Scope of Identifiers/ Slide 2 Scope l A sequence of statements within { … } is considered.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
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.
Local, Global Variables, and Scope. COMP104 Slide 2 Functions are ‘global’, variables are ‘local’ int main() { int x,y,z; … } int one(int x, …) { double.
Miscellaneous topicsCS-2301 B-term Miscellaneous Topics CS-2301, System Programming for Non-majors (Slides include materials from The C Programming.
Overview scope - determines when an identifier can be referenced in a program storage class - determines the period of time during which that identifier.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
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.
Bindings and scope  Bindings and environments  Scope and block structure  Declarations Programming Languages 3 © 2012 David A Watt, University.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition, Fifth Edition Chapter 7: User-Defined Functions II.
1 Scope Scope describes the region where an identifier is known, and semantic rules for this.
1 Simple Functions Writing Reuseable Formulas. In Math Suppose f (x) = 2 x 2 +5Suppose f (x) = 2 x 2 +5 f(5)=?f(5)=? f(5) = 2* =55f(5) = 2*
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.
CPS120: Introduction to Computer Science Functions.
Chapter 6: Programmer- defined Functions Development of simple functions using value and reference parameters JPC and JWD © 2002 McGraw-Hill, Inc. Modified.
CPS120: Introduction to Computer Science Lecture 14 Functions.
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.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
C++ Programming Lecture 11 Functions – Part III By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Chapter 9 Functions Dept of Computer Engineering Khon Kaen University.
C++ / G4MICE Course Session 2 Basic C++ types. Control and Looping Functions in C Function/method signatures and scope.
1 COMS 261 Computer Science I Title: Functions Date: October 12, 2005 Lecture Number: 17.
1 Functions in C++ Default arguments Overloading Inlining Call-by-reference Scope of variables, static, extern namespace new and delete assert.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
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.
Copyright © Curt Hill The Compound Statement C-Family Languages and Scope.
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
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.
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.
L what are executable/non-executable statements l out of the ones below which constructs are executable #include p=3.14; const double PI=3.14; int myfunc(int);
1 Bindings. 2 Outline Preliminaries Scope  Block structure  Visibility Static vs. dynamic binding Declarations and definitions More about blocks The.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
 constant represented by a name, just like a variable, but whose value cannot be changed  The const keyword precedes the type, name, and initialization.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
Introduction to Programming Lecture 6. Functions – Call by value – Call by reference Today's Lecture Includes.
Function PrototypetMyn1 Function Prototype We can declare a function before we use or define it by means of a function prototype. A function prototype.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
CSCE Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.
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 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
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.
T/F  The following code will compile without error. int x = 3, y = 4, z = 5; double k = 3.4; cout
Advanced Programming in C
Chapter 7: User-Defined Functions II
Computing and Statistical Data Analysis Lecture 2
Global & Local Identifiers
Lecture 4-7 Classes and Objects
Multiple Files Revisited
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Namespaces How Shall I Name Thee?.
Local, Global Variables, and Scope
Multiple Files Revisited
Predefined Functions Revisited
What Is? function predefined, programmer-defined
Presentation transcript:

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 declaration: double number; // declare number //... number /= 10; // ok - real division //... number %= 10; // error - int division

Identifiers Names whose meaning is specified by the programmer (in a declaration) are called identifiers. Identifiers serve as the names of: variables:variables: double number; constants:constants: const double PI = ; functions:functions: int f(int x, int y); parameters:parameters: int f(int x, int y); and other non-predefined symbols within a program.

Declarations A declaration statement binds an identifier to a particular meaning. Example: What will be displayed? int x = 1; // x means an int if (true){ double x = 2.5; // x means a double } else { char x = ‘x’; // x means a char } cout << x; // what does x mean?

Identifier Scope That portion of a program where an identifier is bound to a particular meaning is called the scope of that identifier. In a program where the same identifier has different meanings at different places, identifying its meaning at any given place requires us to understand the rules of scope used by C++.

The Local Scope Rule For any identifier declared within a { } block, the scope of that identifier: –begins at its declaration, and –ends at the end of the block. { double number; // number means a double //... } // number is undeclared Such identifiers are described as local to the block.

Example 1 Since the scope of a local ends at the end of its block, different functions can use the same name for different objects without a conflict: int f(int a) { int x; //... } // scope of x local to f ends double g(double b) { double x; //... } // scope of x local to g ends

Example 2 Variables declared within the body of a loop cannot be accessed outside of the loop: //... do { cout << “Enter an int: “; int i; // scope of i begins cin >> i; // i means an int } // scope of i ends while (i > 0); // error: i undeclared //... This is another reason to avoid declaring variables within the body of a loop.

Example 3 In the code fragment we saw earlier: { int x = 1; // x means an int if (true) { double x = 2.5; // x means a double } // double(x) scope ends else { char x = ‘x’; // x means a char } // char(x) scope ends cout << x; // x means an int } // int(x) scope ends

Note C++ permits the same identifier to be redeclared within nested { } blocks, though doing so is not good programming practice, since it makes it harder to determine which meaning is in use. Most compilers will generate a warning when you do so, since the nested declaration “hides” the outer declaration.

The Extern Scope Rule For any identifier declared outside of all { } blocks, the scope of that identifier: –begins at its declaration, and –ends at the end of the file. const double PI = ; int main() { //... } //... // scope of PI ends at end-of-file Such identifiers are described as externs.

Example #include // cin, cout,... int main() { //... } In this fragment, the compiler inserts the declarations of cin, cout, and so on into our program. These declarations are outside of all {} blocks, and so cin, cout,..., are extern identifiers. The scope of cin, cout,... extends to the end of the file, allowing them to be accessed by any function whose definition follows the #include directive.

The Parameter Scope Rule For any identifier declared as a function parameter, its scope: –begins at its declaration, and –ends at the end of the function’s definition. Parameters are considered to be local to their function. int f(int x) { //... } // scope of x ends

Example Since they are considered to be locals, we can use the same name for similar parameters to different functions: int f(int x) { //... // x means an int local to f } // scope of x local to f ends int g(int x) { //... // x means an int local to g } // scope of x local to g ends

The for Loop Rule For any identifier declared as a loop control variable in a for loop, its scope: –begins at its declaration, and –ends at the end of the loop. void f(string & str) { for (int i = 0; i < str.size(); i++) { //... i refers to an int } // scope of int(i) ends } Loop-control variables are considered to be local to their loop.

Example 1 Since loop-control variables are local to the loop, we can re-declare the same name in different loops: void f(string & str) { for (int i = 0; i < str.size(); i++) { //... i refers to an int } // scope of int(i) ends for (double i = 0; i < 100; i += 5) { //... i refers to a double } // scope of double(i) ends }

Example 2 Since loop-control variables are local to the loop, we cannot access such variables outside of the loop: int f(string & str) { for (int i = 0; i < str.size(); i++) { //... i refers to an int } // scope of int(i) ends return i; // error: i is undeclared }

Discussion In addition to these rules of scope, C++ also defines the rules for two others: Class scope, governing the bindings of identifiers declared within classes; andClass scope, governing the bindings of identifiers declared within classes; and Namespace scope, governing the bindings of identifiers declared within namespaces.Namespace scope, governing the bindings of identifiers declared within namespaces. We are not yet ready to discuss either of these, but we will do so when we are ready.

Summary The portion of a program where an identifier is bound to a particular meaning is called the scope of that identifier. An identifier is bound to a meaning by a declaration. C++ provides a variety of contexts called scoping contexts in which identifiers can be declared. In each scoping context, the scope of an identifier declared within that context begins at its declaration.

Summary The scoping contexts differ in when the scope of an identifier declared within them ends: at the end of the block, for local identifiers.at the end of the block, for local identifiers. at the end of the file, for extern identifiers.at the end of the file, for extern identifiers. at the end of the function, for parameter identifiers.at the end of the function, for parameter identifiers. at the end of the loop, for loop-control variable identifiers.at the end of the loop, for loop-control variable identifiers.