In C++, a function must be defined before it is used anywhere in the program. The general form of a function definition is as given below :
type function-name(parameter list)
{body of the function}
where the type specified the type of value that return statement of the function returns. It may be any valid C++ data type. If no type is specified, the compiler assumed that the function returns an integer value. The parameter list is a comma-separated list of variables of a function referred to as its arguments. A function may be be without any parameters, in which case, the parameter list is empty.
|
In C++ language there are two types of functions :
These functions are part of the compiler package. These are part of standard library made available by the compiler. For example: exit(), sqrt(), pow() etc. are library functions (or built-in functions).
The user-defined functions are created by you (the programmer). These functions are created as per requirements of your program.
Function prototyping is one very useful feature of C++ function. A function prototype describes the function interface to the compiler by giving details such as the number and type of arguments and the type of return values.
Thus the above given examples are function definitions and the following are declarations or shall we say, function prototypes :
int absval(int a);
int gdc(int n1, int n2);
Therefore, you can say that a function has following parts :
As you know about void data type that it specifies an empty set of values and it is used as the return type for functions that do not return a value. Thus, a function that does not return a value is declared as follows :
void function-name(parameter list);
By declaring a functions return type void, one makes sure that the function cannot be used in an assignment statement.
A function that does not require any parameter (it has an empty argument list) can be declared as follows :
type function-name(void);
As already mentioned if you omit the type specifier of a function, it is assumed to be returning int values. For the functions returning non-integer, the type specifier must be given.
It's a special area where you can find special questions and answers for CSE students or IT professionals. Also, In this section, we try to explain a topic in a very deep way.