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 89someFunction();// calls someFunction(void)someFunction(intVariable1); // calls someFunction(int)someFunction(doubleVariable); // calls someFunction(double)someFunction(intVariable1, intVariable2); // calls// someFunction(int, int)// this works for constants as wellsomeFunction(1);// calls someFunction(int)someFunction(1.0);// calls someFunction(double)someFunction(1, 2); // calls someFunction(int, int)In each case, the type of the arguments matches the full name of the threefunctions.The return type is not part of the extended name (which is also known as thefunction signature) of the function. The following two functions have the samename — so they can’t be part of the same program:int someFunction(int n); // full name of the function// is someFunction(int)double someFunction(int n); // same nameYou’re allowed to mix variable types as long as the source variable type ismore restrictive than the target type. Thus an int can be promoted to adouble. The following is acceptable:int someFunction(int n);double d = someFunction(10); // promote returned valueThe int returned by someFunction() is promoted into a double. Thus thefollowing would be confusing:int someFunction(int n);double someFunction(int n);double d = someFunction(10);// promote returned int?// or use returned double as isHere C++ does not know whether to use the value returned from the doubleversion of someFunction() or promote the value returned from int version.Defining Function PrototypesThe programmer may provide the remainder of a C++ source file, or module,the extended name (the name and functions) during the definition of thefunction.

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

Saved successfully!

Ooh no, something went wrong!