C++ FUNCTION PROTOTYPING
Function prototyping is one of the major improvements added in C++ functions. The prototype describes the function interface to the compiler by giving details such as type of arguments, No. of arguments and last the type of return values. With function prototyping, a template is always used when declaration and defining a function. When a function is called, the compiler uses the template is always used when declaring and defining a function.
when a function is called, the compiler uses the template to ensure that proper arguments are passed, Any violation in matching the arguments or the return types will be
caught by the compiler at the time of compilation itself.These checks and controls did’nt exist in the conventional C functions.
Remember, C also uses prototyping but it was introduce first in c++ language by stroustrap and the success of this feature inspired the ANSI C committee to adopt it. however there is a major difference in prototyping
between C and C++ language. while c++ makes the prototyping assential, ANSI C makes it optional, perhaps, to preserve the compatibility with classic C
Function prototype is a declaration statement in the calling program and in the following form
type function_name(arguments list);
|
The arguments list contains the types and names of arguments that must be passed to the function
Example
valume(int x, float y, float z);
|
Note the each argument variable must be declared independently inside the parentheses. This is a combined declaration like
volume(int x, float y,z);
|
in a function declaration, the names of the arguments are dummy variable and therefore, they are optional.
is acceptable at the place of declaration. At this stage, the compiler only checks for the type of the arguments when the function is called
In general, we can either exclude or include the variable name in the argument list of prototypes. The variable names in the prototype just act as place holders and therefore, if names are used they don’t have to match the names used in the function cal or function definition.
in the definition of function, names are required because arguments must be referenced inside the function.
Example
valume( a, b, c)
{
v=a*b*c;
……….
……….
}
|
the volume() function can be invoked as follows :
float cube1=volume(b1,w1,h1);
here The variable b1,w1, h1 are known as the actual parameters(arguments) which specify the dimensions of cube1. Their types(which have been declared earlier) should match with the types declared in the function prototype.
Remember, the calling statement should not include names in the following example
In c++ this means that the function does not pass any parameters. it is identical to the statement