11.07.2015 Views

tYSR20

tYSR20

tYSR20

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 6: Creating Functions 85Understanding functions with argumentsSimple functions are of limited use because the communication from suchfunctions is one-way — through the return value. Two-way communicationis through function arguments.Functions with argumentsA function argument is a variable whose value is passed to the calling functionduring the call operation. The following SquareDemo example program definesand uses a function square() that returns the square of a double precisionfloat passed to it:// SquareDemo - demonstrate the use of a function// which processes arguments#include #include #include using namespace std;// square - returns the square of its argument// doubleVar - the value to be squared// returns - square of doubleVardouble square(double doubleVar){return doubleVar * doubleVar;}// sumSequence - accumulate the square of the number// entered at the keyboard into a sequence// until the user enters a negative numberdouble sumSequence(void){// loop foreverdouble accumulator= 0.0;for(;;){// fetch another numberdouble dValue = 0;cout > dValue;// if it’s negative...if (dValue < 0){// ...then exit from the loop

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

Saved successfully!

Ooh no, something went wrong!