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

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

View Full Document Right Arrow Icon
Chapter 8 Scope, Lifetime, and More on Functions Chapter 8 SCOPE, LIFETIME, AND MORE ON FUNCTIONS CHAPTER GOALS To be able to do the following tasks, given a C++ program composed of several functions: determine whether a variable is being referenced globally. determine which variables are local variables. determine which variables are accessible within a given block. To be able to determine the lifetime of each variable in a program. To understand and be able to avoid unwanted side effects. To know when to use a value-returning function. To be able to design and code a value-returning function for a specific task. To be able to invoke a value-returning function properly. CHAPTER OUTLINE I. Scope of Identifiers A. Scope Rules B Variable Declarations and Definitions C. Namespace II. Lifetime of a Variable A. Initializations in Declarations III. Interface Design A. Side Effects B. Global Constants May We Introduce : Ada Lovelace IV. Value-Returning Functions A. Boolean Functions Matters of Style : Naming Value-Returning Functions B. Interface Design and Side Effects C. When To Use Value-Returning Functions Background Information : Ignoring a Function Value D. When to Use Value-Returning Functions V. Problem-Solving Case Study : Reformat Names Software Engineering Tip : Control Abstraction, Functional Cohesion, and Communication Complexity VI. Problem-Solving Case Study : Comparison of Furniture-Store Sales, Part 2 VII. Problem-Solving Case Study : Starship Weight and Balance VIII. Testing and Debugging A. Stubs and Drivers B. Testing and Debugging Hints IX. Summary 130
Background image of page 1

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

View Full DocumentRight Arrow Icon
Chapter 8 Scope, Lifetime, and More on Functions GENERAL DISCUSSION This chapter has three key concepts: scope of an identifier, lifetime of a variable, and value-returning functions. An identifier's scope of access is that part of a program from which the identifier may be referenced. Technically, the scope rules in C++ are more complicated than in Pascal because of C++'s class mechanism, which is explored in Chapter 11. Paradoxically, though, at this point in the course the students tend to have fewer problems with scope than in Pascal, primarily because functions cannot be nested in C++. (It's true that blocks containing declarations might be nested. C++, unlike Pascal, allows variables and constants to be declared inside any block, not only the block that constitutes a function body. However, it is rare to include declarations in arbitrary blocks.) Another simplifying aspect of C++ is that variables declared by the main function are not global variables; they are local to main . No other function can access main 's variables directly. In contrast, variables declared by a Pascal main program are global variables, and students find it tempting to access these variables directly from a procedure or function. The most important point to make about scope of access is that it is a dangerous practice to reference a
Background image of page 2
Image of page 3
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 / 24

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

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

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