C plus plus functions with no parameter

C++ functions with no parameter means the functions for which no input parameters are supplied. Normally, they will not return a value, but carry out some operations. For example consider the following function, which skip three lines on output.

void skipthree(void)
  // skip three lines on output
{
cout<< endl << endl<<endl;              
}

The function-type has been given as void, which tells the compiler that this function does not return any value. Because the function does not take parameters the parameter-list is empty, this is indicated by the void parameter-list No local variables are required by this function and the function implementation only requires the sending of three successive ends of line characters to the output stream cout.
Since this function does not return a value it cannot be used in an expression and is called by treating it as a statement as follows:

skipthree();

Even though there are no parameters the empty parameter list () must be inserted. When a function is called the C++ compiler must insert appropriate instructions into the object code to arrange to pass the actual parameter values to the function code and to obtain any values returned by the function. To do this correctly the compiler must know the type of all parameters and the type of any return value. Thus before processing the call of a function it must already know how the function is defined. This can be done by defining functions before the main program, for example the function skipthree could be incorporated in a program as follows:

C++ functions with no parameter Example:
#include<iostream.h>
void skipthree(void)
// Function to skip three lines
{
cout<< endl<<endl<<endl;
}
void main()
{
int….;
float….;
cout<<”Title Line 1”;
skipthree();
cout<<”Title Line 2”;
}

However this has disadvantages, namely:

  • The main program tends to convey much more information of use in understanding the program than do individual functions. So it is better if the main program comes first. However this means that the compiler meets the call of a function before it meets the definition of the function.
  • If using functions from library of functions then the main program is linked with the pre-compiled object code of the functions. Thus while compiling the main program on its own the compiler has no knowledge of the function definition.
The solution to both the problems is to use Function prototypes. A function prototype supplies information about the return type of a function and the type of its parameters. This function prototype is then placed before the main program that uses the function. The full function definition is then placed after the main program or may be contained in a separate file that is compiled separately and linked to the main program later. The function prototype is merely a copy of the function heading. Function prototype is a declaration statement in the calling program and is of the following form:
C++ functions with no parameter example:
type function-name (argument-list);
Thus the function prototype for the function skipthree
void skipthree(void);
which would be included in the program file as follows:
#include<iostream.h>
void skipthree(void);
// Function prototype
void main()
{
int….;
float….;
cout<<”Title Line 1”;
skipthree();
cout<<”Title Line 2”;
}
// Now the function definition
void skipthree(void)
// Function to skip three lines
{
cout<< endl<<endl<<endl;
}

Leave a Reply

Your email address will not be published.