C++
C++ Return Value in Void Fix
When a function is declared void, it hands nothing back to its caller. If a return statement inside it carries a value - a literal, a computed expression, a variable, or the result of another call - compilation stops with 'return-statement with a value, in function returning void'. The mismatch is between the declaration and the return statement, and one of them is wrong. This guide separates the cases, reads what the diagnostic actually reports, and matches each one to its fix.
Written November 25, 2026 · Tested with GCC 13.3.0 · 9 min read
- The rule underneath: a void function has no value to hand back
- How to read the diagnostic
- The first failure: a literal returned from a void function
- The second failure: an expression returned from a void function
Written November 25, 2026
Tested with GCC 13.3.0
What this guide is
In void show(), the void is a promise: this function returns nothing. A return statement that carries a value breaks that promise, and compilation stops with "return-statement with a value, in function returning 'void'". The value can be anything non-void - the diagnostic does not care whether it is a literal, a computed expression, a variable, or the result of a call.
The causes in this guide each reproduce the failure with a small, complete program: a literal returned from a void function, an arithmetic expression, a local variable, the result of a value-returning call, and a value returned from a lambda whose return type was written as void. In each family the failing program is small, the compiler output is shown, and the fix either changes the function's return type so the value has somewhere to go, or keeps the function void and uses the value inside the function instead of returning it.
Build every program in this guide with g++ -std=c++20 -Wall -Wextra. The failing programs are compiled with -c - compile only - because the error prevents any program from being produced; the fixed programs are compiled and run, so their output is shown.
This guide is about a value on a return inside a void function. It is a different diagnostic from the mirror image - a bare return; inside a function that returns a value - and from a caller trying to use the result of a void call. Both of those appear at the end, with their own wording. One shape that looks like it belongs here is legal, and it matters: returning the result of a call that itself returns void. That shape is also at the end, because mistaking it for this error leads to fixing code that was never broken.
The rule underneath: a void function has no value to hand back
The void type is the type with an empty set of values. A function declared to return void therefore has no value to give its caller - there is no such value to give. That is the whole rule behind this diagnostic: void in the declaration is not a placeholder for "returns something later", it is the return type, and every return in the body has to agree with it.
The return statement terminates the current function and, when it has an operand, returns the result of that operand to the caller after conversion to the function's return type. In a function returning void, the expression form can be used only if the expression itself has void type. A literal, an int variable, or a call returning int does not, so the statement fails. Reaching the end of a void function without a return is fine - a bare return; is executed there in effect - and a bare return; written in the body is fine too.
Three consequences follow. First, the fix is always a choice between the two sides of the mismatch: either the caller needs the value, in which case the declaration is wrong and the return type changes, or the caller does not, in which case the return is wrong and the value is used inside the function, or dropped, instead. Second, changing the value does not help: return 0;, return result;, and return compute(); fail identically, because the problem is that there is a non-void value at all, not which value it is. Third, the [-fpermissive] at the end of GCC's message does not make this a warning to wave through - it marks a place where the compiler could be told to downgrade the error, and relying on that flag hides a declaration that contradicts the code instead of settling which side is right.
That gives a fast way to read any instance of this error: find the function named in the first line of the output, look at its declared return type, then look at the expression after return. If the declaration says void and the expression has a non-void type, decide who needs the value - the caller, or only the function itself - and change the side that is wrong.
How to read the diagnostic
Every failure in the main families prints the same shape of output: the file and the function signature on the first line, then an error line giving the line and column of the value after return, the full message ending in [-fpermissive], and the source line with a caret under the returned value. Read it in order. The function named on the first line - void show(), void doubled(int) - is the declaration to check, and it already tells you the return type is void. The caret does not sit under the word return; it sits under the value, because the value is the part that does not fit. There is no note pointing at the declaration: GCC reports the return site, and reading the declaration at the top of the named function is your job. The wording is also stable across the families - literal, expression, variable, and call all produce the same sentence, so the sentence alone does not tell you which family you have; the expression under the caret does.
The first failure: a literal returned from a void function
A program that tries to hand a fixed number back through a void function:
#include <iostream>
void show() {
return 42;
}
int main() {
show();
std::cout << "done\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_literal.o fail_literal.cpp
fail_literal.cpp: In function ‘void show()’:
fail_literal.cpp:3:12: error: return-statement with a value, in function returning ‘void’ [-fpermissive]
3 | return 42;
| ^~
Line 3 tries to return 42, an int, from a function whose return type is void. The call in main asks for nothing - show(); as a plain statement - so the code disagrees with itself about whether this function produces a value. One side has to change.
Here the value is the point of the function, so the declaration changes. The function returns int, and the caller uses what it gets:
#include <iostream>
int show() {
return 42;
}
int main() {
std::cout << show() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_literal fix_literal.cpp
$ ./fix_literal
42
The output is 42. If nothing in the caller needs the number, the other fix is the right one: keep void, and either print the value inside the function or delete the return line entirely - a void function that reaches its closing brace returns on its own. What does not work is leaving void in place and changing the literal; return 0; fails in exactly the same way.
The second failure: an expression returned from a void function
A program that computes a doubled value and tries to return the computation:
#include <iostream>
void doubled(int n) {
return n * 2;
}
int main() {
doubled(21);
std::cout << "done\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_expr.o fail_expr.cpp
fail_expr.cpp: In function ‘void doubled(int)’:
fail_expr.cpp:3:14: error: return-statement with a value, in function returning ‘void’ [-fpermissive]
3 | return n * 2;
| ~~^~~
The caret spans n * 2, the whole expression. An expression is not treated more leniently than a literal: its type is int, and an int is a value, whatever produced it. Splitting the computation into a variable first would not change anything either - that is the third family below, with the same message.
A function whose job is to compute something is a function that returns it. The declaration changes to int, and the call site takes the result:
#include <iostream>
int doubled(int n) {
return n * 2;
}
int main() {
std::cout << doubled(21) << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_expr fix_expr.cpp
$ ./fix_expr
42
The output is 42, from 21 * 2. Note what changed besides the return type: the call in main could no longer stay a bare statement that discards the answer, because discarding it would make the function pointless even though it would compile. When you change a return type, check the call sites - they are the other half of the contract.
The third failure: a variable returned from a void function
A program that stores a result in a local variable, then returns the variable:
#include <iostream>
void report() {
int result = 7;
return result;
}
int main() {
report();
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_variable.o fail_variable.cpp
fail_variable.cpp: In function ‘void report()’:
fail_variable.cpp:4:12: error: return-statement with a value, in function returning ‘void’ [-fpermissive]
4 | return result;
| ^~~~~~
Naming the value first does not soften it. result has type int, and returning it is returning a value, whether the value was written as a literal on the return line or computed earlier and stored. This shape often appears when a function grows: it started as a print-or-do routine declared void, and a later edit added a result that the author then tried to send back.
In this program the value is only used for the report itself, so the function stays void and uses the variable where it stands:
#include <iostream>
void report() {
int result = 7;
std::cout << result << "\n";
}
int main() {
report();
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_variable fix_variable.cpp
$ ./fix_variable
7
The output is 7. The return line is gone, not rewritten - the function ends at its closing brace, which is a complete way for a void function to finish. If a caller later needs result, that is the moment the return type changes to int and the return result; line comes back; until then, returning a value nobody receives is the contradiction that caused the error.
The fourth failure: the result of a value-returning call
A program where a void function forwards the result of a call that does return a value:
#include <iostream>
int compute() {
return 9;
}
void show() {
return compute();
}
int main() {
show();
std::cout << "done\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_call.o fail_call.cpp
fail_call.cpp: In function ‘void show()’:
fail_call.cpp:6:19: error: return-statement with a value, in function returning ‘void’ [-fpermissive]
6 | return compute();
| ~~~~~~~^~
This is the family most often misread, because the same line with a different inner function is legal - see the neighboring shapes below. What decides it is the inner call's return type. compute() returns int, so return compute(); returns an int from a void function, and it fails. Had compute returned void, the identical line would compile.
Here show exists to pass the computed value on, so its return type follows the value it forwards:
#include <iostream>
int compute() {
return 9;
}
int show() {
return compute();
}
int main() {
std::cout << show() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_call fix_call.cpp
$ ./fix_call
9
The output is 9. If show is meant to stay void - it runs compute for its effect and passes nothing on - the fix is to call it as a statement, compute();, and let show end normally. The line to look at when choosing is the call site in main: if it will use a result, show returns the type compute returns; if it will not, the forwarding return is the part to remove.
The fifth failure: a value returned from a lambda declared void
A program with a lambda whose return type was written out as void, and whose body returns a value:
#include <iostream>
int main() {
auto show = []() -> void {
return 42;
};
show();
std::cout << "done\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_lambda.o fail_lambda.cpp
fail_lambda.cpp: In lambda function:
fail_lambda.cpp:4:16: error: return-statement with a value, in function returning ‘void’ [-fpermissive]
4 | return 42;
| ^~
The first line of the output says "In lambda function" instead of naming a function, but the rule is the same one. The trailing -> void in the lambda is a return-type declaration, with the same force as the void in front of a named function. It is easy to write by habit, or to copy from a lambda that genuinely returned nothing, and then the body outgrows it.
The fix changes the trailing return type to the type the body actually returns:
#include <iostream>
int main() {
auto show = []() -> int {
return 42;
};
std::cout << show() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_lambda fix_lambda.cpp
$ ./fix_lambda
42
The output is 42. The trailing type can also be removed altogether when the lambda has a single return - the return type is then deduced from it, which is the deduced shape in the neighboring section below. Keeping an explicit type is clearer when a lambda has several return statements, because they must all agree on one type.
The fixes in reach order
- The caller needs the value: change the function's return type from
voidto the type of the returned expression, and update the call sites to use the result. - The caller does not need the value: keep
void, use the value inside the function - print it, store it, pass it on as an argument - and delete thereturnwith a value, or replace it with a barereturn;. - The returned thing is the result of a call: check that call's return type. If it returns a value and the outer function only runs it for its effect, call it as a plain statement instead of returning it.
- The function is a lambda: the trailing
-> voidis the declaration to change, or remove it and let the return type be deduced when there is onereturn. - The returned expression itself has
voidtype - a call to avoidfunction, or an expression converted tovoid: there is no error. Do not fix legal code; see the neighboring shapes. - The wording is different - "with no value, in function returning 'int'", or "void value not ignored": that is a neighboring problem, not this one - see the traps.
Traps
- A bare
return;in avoidfunction is legal. It ends the function early and hands back nothing, which is exactly whatvoidpromises:
#include <iostream>
void show(int n) {
if (n < 0) {
return;
}
std::cout << n << "\n";
}
int main() {
show(-1);
show(3);
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_bare trap_bare.cpp
$ ./trap_bare
3
3 only: the call with -1 leaves through the bare return, and the call with 3 prints. An early exit is the main use of return in a void function, and it needs no value after it.
- Returning the result of a call that itself returns void is legal, and it is the neighboring shape most likely to be "fixed" by mistake. The expression after return has void type, which is the one kind of expression a void function may return:#include <iostream>
void inner() {
std::cout << "inner\n";
}
void outer() {
return inner();
}
int main() {
outer();
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_voidcall trap_voidcall.cpp
$ ./trap_voidcall
inner
The output is inner. Compare the fourth family: the line is identical in form, and the only difference is the inner function's return type - void here, int there. When a return someCall(); line is reported, read the called function's declaration before changing anything; when it is not reported, leave it alone.
- An expression explicitly converted to void can also be returned. The conversion discards the value and gives the expression void type, so the statement satisfies the rule - though in most code a plain statement plus a bare return; says the same thing more plainly:
#include <iostream>
void show(int n) {
std::cout << n << "\n";
return static_cast<void>(n);
}
int main() {
show(7);
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_voidcast trap_voidcast.cpp
$ ./trap_voidcast
7
The output is 7, printed before the return. This shape turns up in template code and in macros that forward a call without knowing whether it returns a value; in ordinary code it is legal but rarely the clearest way to write the function.
- A function with no declared return type to contradict compiles fine, because its return type is deduced from the return. The error needs an explicit void - written in front of a named function, or after a lambda's parameter list:
#include <iostream>
auto value() {
return 42;
}
int main() {
std::cout << value() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_auto trap_auto.cpp
$ ./trap_auto
42
The output is 42, and value returns int because 42 is an int. Deduction is a real fix route for a small function written from scratch, but it is a poor repair for an existing void function with callers: changing void show() to auto show() changes the function's contract silently, where writing int show() states it.
- A bare return; in a function that returns a value reports the mirror-image wording, "with no value". It is a different error with the opposite repair - the function owes its caller a value, so the return gains one, or the declaration loses its return type:
#include <iostream>
int value() {
return;
}
int main() {
std::cout << value() << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_novalue.o trap_novalue.cpp
trap_novalue.cpp: In function ‘int value()’:
trap_novalue.cpp:3:5: error: return-statement with no value, in function returning ‘int’ [-fpermissive]
3 | return;
| ^~~~~~
Read the middle of the message, not just its start: "with a value, in function returning 'void'" and "with no value, in function returning 'int'" share most of their words and demand opposite changes.
- A caller that tries to use the result of a void call reports its own error, at the call site rather than inside the function. The function in this program is written correctly; the assignment in main is the broken side:
#include <iostream>
void show() {
std::cout << "hi\n";
}
int main() {
int x = show();
std::cout << x << "\n";
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o trap_usevoid.o trap_usevoid.cpp
trap_usevoid.cpp: In function ‘int main()’:
trap_usevoid.cpp:6:17: error: void value not ignored as it ought to be
6 | int x = show();
| ~~~~^~
This is the same underlying mismatch seen from the other end: a caller expects a value that the declaration says does not exist. If the caller genuinely needs a value, this is the evidence that the function's return type should change - fix it at the declaration, and the return inside the function will then need a value of that type.
Checklist
- The function named in the diagnostic has been read at its declaration, and its return type is known.
- The expression after the failing
returnhas been identified - literal, expression, variable, or call - and, if it is a call, that call's return type has been checked. - The intent is settled: the caller either receives the value (the return type changes from
void) or it does not (the function staysvoidand the value is used inside it, or dropped) - not both. - Every call site has been checked after a return-type change, so no caller still discards a result the function now exists to produce, and none tries to use a result from a function that stayed
void. - A lambda's trailing return type agrees with its body, or the trailing type has been removed and the deduction checked.
- A legal shape - a bare
return;, a returnedvoidcall, a returned expression converted tovoid- has been left alone. - A "with no value" or "void value not ignored" wording has been fixed as the neighboring problem it is, at the side the wording names.
- The fixed program run, and its output checked against what the program was meant to do.
Related guides on this site
- Void Value Not Ignored Fix
- 'std::function' Is Not a Member of 'std' Fix
- C++ Invalid Types Array Subscript Fix
- C++ Invalid Conversion Void* Fix
Sources
- cppreference, return statement - for the return statement terminating the function and returning the operand's result to the caller, for the expression form being usable in a function returning void only when the expression has void type, and for reaching the end of a void function acting as a bare return.
- cppreference, Fundamental types - for void being the type with an empty set of values, for objects of type void being disallowed, and for functions returning void being permitted.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.