11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 6Creating FunctionsIn This Chapter Writing functions Passing data to functions Naming functions with different arguments Creating function prototypes Working with include filesThe programs developed in prior chapters have been small enough thatthey can be easily read as a single unit. Larger, real-world programs canbe many thousands (or millions!) of lines long. Developers need to break upthese monster programs into smaller chunks that are easier to conceive,develop, and maintain.C++ allows programmers to divide their code into exactly such chunks(known as functions). As long as a function has a simple description and awell-defined interface to the outside world, it can be written and debuggedwithout worrying about the code that surrounds it.This divide-and-conquer approach reduces the difficulty of creating a workingprogram of significant size. This is a simple form of encapsulation — seeChapter 15 for more details on encapsulation.Writing and Using a FunctionFunctions are best understood by example. This section starts with theexample program FunctionDemo, which simplifies the NestedDemo programI discussed in Chapter 5 by defining a function to contain part of the logic.Then this section explains how the function is defined and how it is invoked,using FunctionDemo as a pattern for understanding both the problem andthe solution.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!