Converting values to lower fundamental types can result in incorrect values. Therefore, a value can be converted to a lower fundamental type only by explicitly assigning the value to a variable of lower type (some compilers will issue a warning in this case) or by using a cast operator (see Section 4.7). Function argument values are converted to the parameter types in a function prototype as if they were being assigned directly to variables of those types. If a square
function that uses an integer parameter is called with a floating-point argument, the argument is converted to int
(a lower type), and square
could return an incorrect value. For example, square(4.5)
returns 16
, not 20.25
.
Common Programming Error 6.4
It’s a compilation error if the arguments in a function call do not match the number and types of the parameters declared in the corresponding function prototype. It’s also an error if the number of arguments in the call matches, but the arguments cannot be implicitly converted to the expected types.