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 6: Creating Functions 83Enter next number: 3Enter next number: -1The total is 6Enter next sequenceEnter next number: 1Enter next number: 2Enter next number: -1The total is 3Enter next sequenceEnter next number: -1Thank youPress any key to continue . . .Understanding the Details of FunctionsFunctions are so fundamental to creating C++ programs that getting a handleon the details of defining, creating, and testing them is critical. Armed withthe example FunctionDemo program, consider the following definition offunction:A function is a logically separated block of C++ code. The function constructhas the following form: name(){// ...return ;}The arguments to a function are values that can be passed to the function tobe used as input information. The return value is a value that the functionreturns. For example, in the call to the function square(10), the value 10 isan argument to the function square(). The returned value is 100.Both the arguments and the return value are optional. If either is absent, thekeyword void is used instead. That is, if a function has a void argument list,the function does not take any arguments when called (this was the case withthe FunctionDemo program). If the return type is void, the function does notreturn a value to the caller.In the example FunctionDemo program, the name of the function issumSequence(), the return type is int, and no arguments exist.

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

Saved successfully!

Ooh no, something went wrong!