Like variables, functions also need to be declared before they are used in programs. A function declaration is also known as function prototype. Function prototype is a model or a blueprint for a function that informs the C++ compiler about the return type, the function name, and the number and data type of the arguments passed to the function. Function name together with parameter list is known as function signature and it does not include return type of a function. The syntax for declaring a function is
return_type function_name(parameter_list);
where,
return_type = the data type of the value returned by the function(by default, it is int)
function_name = the name of the function
(parameter_list) = the list ofvariab1es along with their data types
The syntax for specifying a function’s parameter list is
typel param1, type2 param2, … , typeN paramN
where,
type1 type2, typeN = data types of parameters – cannot be void
paraml, param2, … , paramN = list of variables (also known as parameters)
To understand the concept of function declaration, consider this statement.
int func(int x, char y);
In this statement, a function named funds declared which returns an integer value and takes an integer and a character value as parameters: Note that in function prototype, data types of parameters must be specified, however, specifying parameter names is optional. This is because the compiler only needs to be aware of the data type of every parameter. Thus, the prototype of func can also be written as
int func(int, char);
These arguments are known as fixed arguments and are type checked at compile time.
The function whose parameter_list is empty or void can be declared as
void disp () ;
It can also be written as
void disp (void) ;
Function prototype enables the compiler to validate a function call. Whenever a function is called, the compiler checks whether the return type, the function name, and the type and order of parameters are compatible with the function declaration or not. In case a conflict arises, an error is raised. In addition, function prototype forces the conversion of arguments to the appropriate type. For example, the sqrt function of header cmath specifies a double argument in its function prototype. However, this function can also be called with an int argument and the function will work correctly because of this implicit conversion.
To understand the concept of implicit type conversion of arguments, consider this statement.
cout<<sqrt(4) ;
This statement prints the value 2 as the function prototype will force the compiler to convert integer argument (4)to double argument (4. 0)and then double argument (4. 0) will be passed to sqrt.
Open Parameter List: Generally, a function accepts a fixed number of parameters. However, C++ also enables to provide variable or open number of parameters in parameter list. Such functions must have at least one fixed argument. If this mandatory argument is not provided, the other arguments in the list cannot be accessed. The last argument of this function should be ellipsis(…), which indicates that the function may take any number of arguments after the mandatory argument.
To understand the concept of open parameter list, consider this statement.
int myfunc (int count, long total, ... );
In this statement, count and total are the fixed arguments that must be passed whenever the function is called. However, the variable arguments specified by ellipsis(…) may or may not be passed. Note that the variable arguments are passed with no type checking.