C++ uses the same format of function definition as defined in the ANSI C standard. The
types of the parameters are specified within the round brackets following the function name.
C++ function declarations are used to provide checking of the return type and parameters
for a function that has not yet been defined. C++ declarations are the same as ANSI C
prototypes.
The advantage of using function declarations and the new style definitions is that C++ will
check that the type of the actual parameters are sensible —not necessarily the same. Either
the types must be the same, or all the applicable standard and user defined type conversions
are tried to see if a match can be found.
Some C++ implementations will not accept the old K&R declaration style at all.
double minimum(double a, double b)
// C++ and ANSI C function definition
{
return a < b ? a : b;
}
double maximum(a, b)
// old fashioned K&R format
double a, b;
{
return a > b ? a : b;
}
int main()
{
minimum(1, 2);
// correct usage minimum(1.0, 2.0);
// C++ will convert 1 to 1.0 and 2 to 2.0
minimum("hi", "there");
// non-sensical, C++ will complain
maximum(1, 2);
// call traditional C function
// no errors - wrong answer
maximum("hi", "there");
// again no complaints - strange results
}
Note that an ANSI C function declaration (where information about the parameters is
omitted) is interpreted by C++ as a declaration of a function that takes no parameters. For
example, the following declaration states that fred returns an int and can take any
number of parameters of any type in ANSI C, whilst in C++ it declares a function that
returns int and takes no parameters in C++.
int fred();
types of the parameters are specified within the round brackets following the function name.
C++ function declarations are used to provide checking of the return type and parameters
for a function that has not yet been defined. C++ declarations are the same as ANSI C
prototypes.
The advantage of using function declarations and the new style definitions is that C++ will
check that the type of the actual parameters are sensible —not necessarily the same. Either
the types must be the same, or all the applicable standard and user defined type conversions
are tried to see if a match can be found.
Some C++ implementations will not accept the old K&R declaration style at all.
double minimum(double a, double b)
// C++ and ANSI C function definition
{
return a < b ? a : b;
}
double maximum(a, b)
// old fashioned K&R format
double a, b;
{
return a > b ? a : b;
}
int main()
{
minimum(1, 2);
// correct usage minimum(1.0, 2.0);
// C++ will convert 1 to 1.0 and 2 to 2.0
minimum("hi", "there");
// non-sensical, C++ will complain
maximum(1, 2);
// call traditional C function
// no errors - wrong answer
maximum("hi", "there");
// again no complaints - strange results
}
Note that an ANSI C function declaration (where information about the parameters is
omitted) is interpreted by C++ as a declaration of a function that takes no parameters. For
example, the following declaration states that fred returns an int and can take any
number of parameters of any type in ANSI C, whilst in C++ it declares a function that
returns int and takes no parameters in C++.
int fred();
No comments:
Post a Comment