CS110 Lab: C++ Void Functions

The purpose of this web page is to help you in using user defined functions in C++.

Function is a very important concept in C++. Every C++ program must have a main function which is what you have been using so far. The C++ compiler looks for the main function when it compiles the program. If the main function is not found, a compiler error will occur. You already know the following format that is used for the main function.

int main()
{
	.
	.
	.

	return 0;
}

This function returns a single value 0. Thus, it is a value-returning function. The return data type is int. The statement return 0; is used to complete the function and the value 0 is returned to the operating system if the function has been processed correctly. The return indicates the end of the function.

Some functions we have used, such as open() in InData.open() and get() in cin.get(), do not return any values. It is only the name of an action. These kind of functions are called void functions.

The C++ system includes a standard library - a large collection of prewritten functions, data types, and other items that any C++ programmer may use. To use library functions, you have to place an #include directive near the top of the program, specifying the header file. For example, to use cin and cout, you have to have #include<iostream> in your program; to use sqrt(x) and pow(x, y) functions, you have to have #include<cmath> in your program.

In this lab, we focus on user defined functions. User defined functions are often declared before they are actually used/called. This forward declaration is also known as a function prototype. The general syntax of a function prototype is:

returnDataType functionName(DataTypeOfParameterList);
If a function is a value-returning function, the returnDataType will be something like int, float, or char and so on. If the function does not return any value, we use the word void for the return data type. Once a function is declared, it must be defined somewhere in the program. The definition of a function has the following general format:
returnDataType functionName(DataTypeWithParameterList)
{
	Statement
	    .
	    .
	    .
}

Now, let's look at void functions.

Void Functions

Here is the syntax template of a void function prototype:

void functionName(DataTypeOfParameterList);

Here is the syntax template of a void function definition:

void functionName(DataTypeWithParameterList)
{
	Statement
	    .
	    .
	    .
}

A void function has a heading that names the function followed by a pair of parentheses. The function identifier/name is preceded by the word void. The parameter list with the corresponding data type is within the parentheses. The body of the function is between the pair of braces.

Example

// Program Stars prints NUM_STARS on the screen.

#include <iostream>
using namespace std;

const int  NUM_STARS = 10;

void PrintStars();  //Function prototype, no parameter
// Prints NUM_STARS stars on the screen.

int main ()
{
    cout  << "The next line contains "  << NUM_STARS
          << " stars. "  << endl;
    PrintStars();  //Function call
    PrintStars();  //Function call
    return 0;
}

//********************************************************
//Function definition, no parameter
//********************************************************

void PrintStars ()
// Post: NUM_STARS asterisks are sent to cout.

{
    cout << "**********"  << endl;
    return;
}                                                               

In the above example, void PrintStars() is the function header. There is no parameter list, but the parentheses that enclose the parameter list must be present. The braces enclose the body of the function, the action part of the function. The output statement prints 10 stars. The return statement ends the function, but it is not required in void functions.

In the main function, PrintStars() is invoked/called and used as a statement to print out the 10 stars. Now, let's look at Value-returning functions.



Copyright: Department of Computer Science, University of Regina.