In a prior lesson (2.1 -- Introduction to functions), we indicated that the syntax for a function definition looks like this:
return-type 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 call a function by itself (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:
Early returns
A return statement that is not the last statement in a function is called an early return. Such a statement will cause the function to return to the caller when the return statement is executed (before the function would otherwise return to the caller, hence, “early”).
In the above example, when print()
executes, it will first print “A”. Then the return statement executes, and control returns back to the caller (main
). “B” is never printed because the statement never executes.
As an aside…
When compiling the above example, your compiler may issue a warning about line 9 being unreachable. If you are treating warnings as errors, you may need to temporary disable that setting in order to compile the example.
Early returns can be used in value-returning functions too:
The above program prints the following:
A5
First, print()
is called. The first statement in print()
prints “A”. Then the return statement is executed, returning the value of 5
back to the caller. This return value is also printed. The statement std::cout << "B"
is never executed because the function has returned to the caller prior to that point.
Historically, early returns were frowned upon. However, in modern programming they are more accepted, particularly when they can be used to make a function simpler, or are used to abort a function early due to some error condition.
Related content
We discuss the debate over early returns in lesson 7.10 -- Break and continue