next up previous
Next: Functions with parameters and Up: Introduction to User-defined functions Previous: Introduction to User-defined functions

Functions with no parameters

Functions with no parameters are of limited use. Usually they will not return a value but carry out some operation. For example consider the following function which skips three lines on output.

void skipthree(void)
   // skips three lines on output
   {
    cout << endl << endl << endl;
   }
Download program.
Note that the function-type has been given as void, this tells the compiler that this function does not return any value. Because the function does not take any 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 end of line characters to the output stream cout. Note the introductory comment that describes what the function does. All functions should include this information as minimal comment.

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 types 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 any functions that are used in the main program before the main program, for example the function skipthree could be incorporated in a program as follows:

#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 way round both the problems above is to use Function prototypes. A function prototype supplies information about the return type of a function and the types 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. Thus the function prototype for the function skipthree is:

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;
  }
Download program.

In fact when using functions from the stream libraries and the mathematical libraries prototypes are required for these functions. This is handled by including the files iostream.h and math.h which, among other things, contain the function prototypes.


next up previous
Next: Functions with parameters and Up: Introduction to User-defined functions Previous: Introduction to User-defined functions
Peter JB King
1999-08-31