In a prior lesson (2.1 -- Introduction to functions), we indicated that the syntax for a function definition looks like this:
returnType identifier() // identifier replaced with the name of your function { // Your code here }
Although we showed examples of functions that had return-type void
, we did not discuss what this meant. In this lesson, we’ll explore functions with a return type of void
.
Void return values
Functions are not required to return a value back to the caller. To tell the compiler that a function does not return a value, a return type of void is used. For example:
In the above example, the printHi
function has a useful behavior (it prints “Hi”) but it doesn’t need to return anything back to the caller. Therefore, printHi
is given a void
return type.
When main
calls printHi
, the code in printHi
executes, and “Hi” is printed. At the end of printHi
, control returns to main
and the program proceeds.
A function that does not return a value is called a non-value returning function (or a void function).
Void functions don’t need a return statement
A void function will automatically return to the caller at the end of the function. No return statement is required.
A return statement (with no return value) can be used in a void function -- such a statement will cause the function to return to the caller at the point where the return statement is executed. This is the same thing that happens at the end of the function anyway. Consequently, putting an empty return statement at the end of a void function is redundant:
Best practice
Do not put a return statement at the end of a non-value returning function.
Void functions can’t be used in expression that require a value
Some types of expressions require values. For example:
In the above program, the value to be printed needs to be provided on the right-side of the std::cout <<
. If no value is provided, the compiler will produce a syntax error. Since the second call to std::cout
does not provide a value to be printed, this causes an error.
Now consider the following program:
The first call to printHi()
is called in a context that does not require a value. Since the function doesn’t return a value, this is fine.
The second function call to function printHi()
won’t even compile. Function printHi
has a void
return type, meaning it doesn’t return a value. However, this statement is trying to send the return value of printHi
to std::cout
to be printed. std::cout
doesn’t know how to handle this (what value would it output?). Consequently, the compiler will flag this as an error. You’ll need to comment out this line of code in order to make your code compile.
Tip
Some statements require values to be provided, and others don’t.
When we have a statement that consists of just a function call (e.g. the first printHi()
in the above example), we’re calling a function for its behavior, not its return value. In this case, we can call either a non-value returning function, or we can call a value-returning function and just ignore the return value.
When we call a function in a context that requires a value (e.g. std::cout
), a value must be provided. In such a context, we can only call value-returning functions.
Returning a value from a void function is a compile error
Trying to return a value from a non-value returning function will result in a compilation error:
Quiz time
Question #1
Inspect the following programs and state what they output, or whether they will not compile.
1a)
1b)