CHAP08 - Chapter 8 Scope, Lifetime, and More on Functions

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Chapter 8 Scope, Lifetime, and More on Functions Dale/Weems/Headington
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Chapter 8 Topics Local Scope vs. Global Scope of an Identifier Detailed Scope Rules to Determine which Variables are Accessible in a Block Determining the Lifetime of a Variable Writing a Value-Returning Function for a Task Some Value-Returning Functions with Prototypes in Header Files cctype and cmath Creating and Using a Module Structure Chart Stub Testing a Program
Background image of page 2
3 Scope of Identifier the scope of an identifier (or named constant) means the region of program code where it is legal to use that identifier for any purpose
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Local Scope vs. Global Scope the scope of an identifier that is declared inside a block (this includes function parameters) extends from the point of declaration to the end of the block the scope of an identifier that is declared outside of all namespaces, functions and classes extends from point of declaration to the end of the entire file containing program code
Background image of page 4
5 const float TAX_RATE = 0.05 ; // global constant float tipRate ; // global variable void handle ( int, float ) ; // function prototype using namespace std ; int main ( ) { int age ; // age and bill local to this block float bill ; . // a, b, and tax cannot be used here . // TAX_RATE and tipRate can be used handle (age, bill) ; return 0 ; } void handle (int a, float b) { float tax ; // a, b, and tax local to this block . // age and bill cannot be used here . // TAX_RATE and tipRate can be used }
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 Detailed Scope Rules 1 Function name has global scope. 2 Function parameter scope is identical to scope of a local variable declared in the outermost block of the function body. 3 Global variable (or constant) scope extends from declaration to the end of the file, except as noted in rule 5. 4 Local variable (or constant) scope extends from declaration to the end of the block where declared. This scope includes any nested blocks, except as noted in rule 5. 5 An identifier’s scope does not include any nested block that contains a locally declared identifier with the same name ( local identifiers have name precedence ).
Background image of page 6
7 Name Precedence Implemented by Compiler Determines Scope When an expression refers to an identifier, the compiler first checks the local declarations. If the identifier isn’t local, compiler works outward through each level of nesting until it finds an identifier with same name. There it stops. Any identifier with the same name declared at a level further out is never reached. If compiler reaches global declarations and still can’t find the identifier, an error message results.
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Namespace Scope the scope of an identifier declared in a namespace definition extends from the point of declaration to the end of the namespace body, and its scope includes the scope of a using directive specifying that namespace
Background image of page 8
9 3 Ways to Use Namespace Identifiers use a qualified name consisting of the namespace, the scope resolution operator ::
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/13/2011 for the course CSC 140 taught by Professor Lebre during the Spring '04 term at Moraine Valley Community College.

Page1 / 44

CHAP08 - Chapter 8 Scope, Lifetime, and More on Functions

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online