3 SCOPE RULES 117LOCAL VARIABLES 117PROCEDURAL ABSTRACTION 120GLOBAL...

3.3

SCOPE RULES 117Local Variables 117Procedural Abstraction 120Global Constants and Global Variables 121Blocks 124Nested Scopes 124Tip: Use Function Calls in Branching and Loop Statements 125Variables Declared in a for Loop 125CHAPTER SUMMARY 126ANSWERS TO SELF-TEST EXERCISES 127PROGRAMMING PROJECTS 130

this page intentionally blank)

3 Function Basics

Good things come in small packages.Common saying

I

NTRODUCTION

If you have programmed in some other language, then the contents of this

chapter will be familiar to you. You should still scan this chapter to see the C++

syntax and terminology for the basics of functions. Chapter 4 contains the

material on functions that might be different in C++ than in other languages.

A program can be thought of as consisting of subparts such as obtaining

the input data, calculating the output data, and displaying the output data.

C++, like most programming languages, has facilities to name and code each

of these subparts separately. In C++ these subparts are called

functions

. Most

programming languages have functions or something similar to functions,

although they are not always called by that name in other languages. The

terms

procedure

,

subprogram

, and

method

, which you may have heard before,

mean essentially the same thing as

function

. In C++ a function may return a

value (produce a value) or may perform some action without returning a

value, but whether the subpart returns a value or not, it is still called a func-

tion in C++. This chapter presents the basic details about C++ functions.

Before telling you how to write your own functions, we will first tell you how

to use some predefined C++ functions.

Predefined Functions