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

Info icon This 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
Image of page 1

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

View Full Document Right 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.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern