Introduction
Function Prototypes
One of the most important features of C++ is the function prototypes. A function prototype tells the compiler the name of the function, the type of data returned by the function, the number of parameters the function expects to receive, the types of the parameters, and the order in which these parameters are expected. The compiler use function prototypes to validate function calls. Early versions of C did not perform this kind of checking, so it was possible to call functions improperly without the compiler detecting the errors Such calls could result in fatal execution-time errors or nonfatal fatal errors that caused, difficult to detect logic errors. Function prototypes correct this deficiency.
The function prototype for maximum in this program is
int maximum( int, int, int);
------------------------------
<P><BR>
<P>
This prototype states that maximum takes three arguments of type int, and returns a result of type int. Notice that this function prototype is the same as the header of the function definition of maximum except the names of the parameters(x, y,and z) are not included.
The portion of a function prototype that includes the name of the function and the types of its argumnets is called the function signture or simply the signture. The function signture dose not include the return type of the function.
// A function call that dose not match the function prototype is a syntax error.
As an example of the preceding Common Programming error, in the previous program, if the function prototype had been written
void maximum( int, int, int);
the compiler would report an error because the void return type in the function prototype would differ from the int return type in the function header.
Another important feature of function prototypes is the coercion of arguments, i.e, the forcing of arguments to the appropriate type. For example, the math library function (sqrt) can be called with an integer argument even though the function prototype in math.h speifies a double argument, and the function will still work correctly. The statement
cout << sqrt( 4 );
correctly evaluates sqrt(4), and prints the value 2. The function prototype causes the compiler to convert the integer argument value 4 to the double value 4.0 before the value is passed to sqrt. In general, argumnet values that do not correspond percisely to the parameter types in the function prototype are converted to the proper type before the function is called. These conversions can lead to incorrect results if C++'s promotion rules are not followed.
the Promotion rules specify how types can be converted to other types without losing data. In our sqrt example above, an int is automatically converted to a double without changing its value. However, a double value converted to an int truncates the fractional part of the double value. Converting large integer to small integer types (e.g, long to short) may also result in changed values.
The promotion rules apply to expressions containing values of two or more data types; such expressions are also referred to as mixed-type expressions. The type of each value in a mixed-type expression is promoted to the "highest" type in the expression. Another common use of promotion is when the type of an argument to a function dose not match the parameter type specified in the function definition. Here is a list of the built-in data types in order from "highest type" to "lowest type" :
---------------------------------
Promotion hierarchy for built-in data types..
long double
double
float
unsigned long int (synonymous with unsigned long)
long int (synonymous with long)
unsigned int (synonymous with unsigned)
int
unsigned short int (synonymous with unsignd short)
short int (synonymous with short)
unsigned Char
short
Char
---------------------------------
Converting values to lower types can result in incorrect values. Therefore, a value can only be converted to a lower type
*/ Converting from a higher data type in the promotion hierarchy to a lower type can change the data value.*/
by explicitly assigning the value to a variable of lower type, or by using a cast operator. Function argument values are converted to the parameter types in a function prototype as if they are being assigned directly to variables of those types. If our square function that uses an integer parameter
<P>#include <iostream.h><BR> <BR> int square( int );
<P>
is called with a floating-point argument, the argument is converted to int (lower type), and square usually returns an incorrect value. For example, square(4.5) would return 16 not 20.25
This member doesn't quite have enough reputation to be able to display their biography and homepage.