void Functions in C++

A void function does not return a value.

The preceding section gives an example of a value-returning function. This section shows how to define and invoke a void function. Listing 6.2 gives a program that defines a function named printGrade and invokes it to print the grade for a given score.

Listing 6.2 TestVoidFunction.cpp

1 #include <iostream>
2
using namespace std;
3
4
// Print grade for the score
5 void printGrade(double score)
6 {
7     
if (score >= 90.0)
8     cout <<
‘A’ << endl;
9     
else if (score >= 80.0)
10    cout <<
‘B’ << endl;
11   
else if (score >= 70.0)
12    cout <<
‘C’ << endl;
13   
else if (score >= 60.0)
14    cout <<
‘D’ << endl;
15   
else
16    cout << ‘F’ << endl;
17 }
18
19
int main()
20 {
21     cout <<
“Enter a score: “;
22     
double score;
23     cin >> score;
24

25     cout << “The grade is “;
26     printGrade(score);
27
28     
return 0;
29 }

The printGrade function is a void function. It does not return any value. A call to a void function must be a statement. So, it is invoked as a statement in line 26 in the main function. Like any C++ statement, it is terminated with a semicolon.

To see the differences between a void and a value-returning function, let us redesign the printGrade function to return a value. We call the new function that returns the grade, as shown in Listing 6.3, getGrade.

Listing 6.3 TestReturnGradeFunction.cpp

1 #include <iostream>
2
using namespace std;
3
4
// Return the grade for the score
5 char getGrade(double score)
6 {
7   
if (score >= 90.0)
8     
return ‘A’;
9   
else if (score >= 80.0)
10     
return ‘B’;
11   
else if (score >= 70.0)
12     
return ‘C’;
13   
else if (score >= 60.0)
14     
return ‘D’;
15   
else
16     return ‘F’;
17 }
18
19
int main()
20 {
21     cout <<
“Enter a score: “;
22     
double score;
23     cin >> score;
24
25     cout <<
“The grade is “;
26     cout << getGrade(score) << endl;
27
28     
return 0;
29 }

The getGrade function defined in lines 5-17 returns a character grade based on the numeric score value. The caller invokes this function in line 26.

The getGrade function can be invoked by a caller wherever a character may appear. The printGrade function does not return any value. It must be invoked as a statement.

Note

A return statement is not needed for a void function, but it can be used for terminating the function and returning control to the function’s caller. The syntax is simply

return;

This is rare but sometimes is useful for circumventing the normal flow of control in a void function. For example, the following code has a return statement to terminate the function when the score is invalid.

// Print grade for the score

void printGrade (  score)

{

if (score < 0 || score > 100)

{

cout << “Invalid score”; return;

}

if (score >= 90.0)

cout << ‘A’;

else if (score >= 80.0)

cout << ‘B’ ;

else if (score >= 70.0)

cout << ‘C’ ;

else if (score >= 60.0)

cout << ‘D’ ;

else

cout << ‘F’ ;

}

Note

Occasionally you may need to terminate the program from the function immediately if an abnormal condition occurs. This can be done by invoking the exit(int) function defined in the cstdlib header. You can pass any integer to invoke this function to indicate an error in the program. For example, the following function terminates the program if an invalid score is passed to the function.

// Print grade for the score

void printGrade (double score)

{

if (score < 0 || score > 100)

{

cout << “Invalid score” << endl;

exit(1);

}

if (score >= 90.0)

cout << ‘A’ ;

else if (score >= 80.0)

cout << ‘B’ ;

else if (score >= 70.0)

cout << ‘C’ ;

else if (score >= 60.0)

cout << ‘D’ ;

else

cout << ‘F’ ;

}

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *