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.

80 Part II: Becoming a Functional C++ ProgrammerThe NestedDemo program in Chapter 5 contains an inner loop (which accumulatesa sequence of numbers) surrounded by an outer loop (which repeatsthe process until the user quits). Separating the two loops would simplify theprogram by allowing the reader to concentrate on each loop independently.The following FunctionDemo program shows how NestedDemo can be simplifiedby creating the function sumSequence().Function names are normally written with a set of parentheses immediatelyfollowing the term, like this:// FunctionDemo - demonstrate the use of functions// by breaking the inner loop of the// NestedDemo program off into its own// function#include #include #include using namespace std;// sumSequence - add a sequence of numbers entered from// the keyboard until the user enters a// negative number.// return - the summation of numbers enteredint sumSequence(void){// loop foreverint accumulator = 0;for(;;){// fetch another numberint value = 0;cout > value;// if it’s negative...if (value < 0){// ...then exit from the loopbreak;}}// ...otherwise add the number to the// accumulatoraccumulator= accumulator + value;}// return the accumulated valuereturn accumulator;int main(int nNumberofArgs, char* pszArgs[]){

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

Saved successfully!

Ooh no, something went wrong!