The Main Function
C language does not specify any return type for the main () function which is the starting point in the execution of the program. The main() definition looks something like this in C -
main()
{
// main program statements and instructions
}
This is perfectly valid in C as main() is C doesnt return any value.
In contrast C++ has a return value of type int for the main () to the Operating System. The main function in C++ is, therfore defined as follows :-
int main ()
{
//program statements
return 0;
}
Note :- Since the return type of functions is int by default, the keyword int in the main is optional.
Many Operating systems test the return value to determine if there is any problem. The normal convention is that an exit value of zero means that the program ran successfully, while a nonzero value means there was a problem. The explicit use of a return 0 statement will indicate that the program was successfully executed.
Function Prototyping
Function prototyping is one of the major improvements added to C++ functions from C functions. The prototype describes the function interface to the computer by giving details such as the number and type of arguments and the type of the return value. With function prototyping , a 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, and the return value is treated correctly. These checks and controls didnt exist in the conventional C functions.
Function prototype is a declaration statement in the calling program and is of the following form
type function-name (argument list );
The argument list contains the types and names of arguments that must be passed to the function.
Example
float volume (int x, float y, float z);
Note :- Each argument variable must be declared independantly inside the parentheses i.e. a combined declaration like
float volume (int x, float y, z) ; is illegal.
In a function declaration, the names of the arguments are dummy variables and therefore they are optional. That is , the form
float volume (int , float, float );
is acceptable at the place of the abovemetioned declaration. At this stage, the compiler only checks for the type of arguments when the function is called.
In the function definition though , names are required because arguments must have a reference inside a function.
Example :-
float volume ( int a, float b, float c)
{
float v = a*b*c;
.........................
.........................
}
the function volume () can be invoked in a program as follows :
float cube1= volume (b1,w1,h1); //Calling the function
The variables b1, w1, h1 are known as actual parameters which specify the dimensions of cube1. Their types should match with the types declared in the protoype.
We can also declare a function with an empty argument list, as in the following example :-
void display () ;
In C++, this means that the function does not pass any parameters. It is identical to the statement
void display ( void);
In C an empty parantheses implies any number of argument generally returning garbage values, in C++ though we have a default void type if the parantheses is left empty.
Hope to see you again for the next topic.
Note :- Each argument variable must be declared independantly inside the parantheses.
No comments:
Post a Comment
Thank you for commenting.