HOWTO · C++
How to Solve Control Reaches End of Non-Void Function Error in C++
Learn why a C++ non-void function can reach its closing brace, how to cover every return path, and when a fallback value or compiler warning helps.
On this page
A C++ function whose return type is not void must produce a value on every reachable path, except that reaching the end of main implicitly returns 0. If a reachable path in any other non-void function reaches its closing brace without a return value, the behavior is undefined; a compiler may also diagnose the path, depending on the code and warning settings. To fix the issue, trace each branch and make its return match the function’s purpose.
Understanding the Error
A non-void function promises a value of its declared type. The compiler diagnostic commonly called “control reaches end of non-void function” points to a path that can reach the closing brace without returning that value. The problem is not simply a missing statement at the bottom: an earlier conditional can leave some inputs without a result. For example, int add(int a, int b) { if (a > b) return a + b; } has no returned value when a <= b. Reaching the end of a non-void function other than main this way has undefined behavior; it does not guarantee a particular crash or result.
Run this example:
#include <iostream>
int add(int a, int b) {
return a + b;
}
int main() {
std::cout << add(5, 3) << '\n';
}
8
The program returns the sum for this input. The inline version above shows the diagnostic cause; the runnable version fixes it by returning a value for every input.
Ensuring All Paths Return a Value
Trace the conditions and decide what each case should mean. For addition, both orders of the operands should produce their sum. If the condition is needed for other work, a final return can cover the remaining path; if both branches do the same work, remove the conditional:
#include <iostream>
int add(int a, int b) {
if (a > b) {
return a + b;
}
return a + b;
}
int main() {
std::cout << add(3, 5) << '\n';
}
A complete branch structure is useful when cases have different meanings. This example returns the larger operand and covers both paths:
#include <iostream>
int larger(int a, int b) {
if (a > b) {
return a;
} else {
return b;
}
}
int main() {
std::cout << larger(5, 3) << '\n';
}
Check nested conditions and switch statements as well: each route through a non-void function must return a value or otherwise not reach the closing brace.
Using Default Return Values
A final return can serve as a fallback for cases that do not match an earlier condition. Use one only when that value represents the function’s contract. For example, this lookup returns a known score for a matching name and 0 when the name is absent; that fallback is appropriate only if the application defines an absent score as zero.
#include <iostream>
#include <string>
int scoreFor(const std::string& name) {
if (name == "Ada") {
return 10;
}
return 0;
}
int main() {
std::cout << scoreFor("Ada") << '\n';
std::cout << scoreFor("Lin") << '\n';
}
If absence is a distinct outcome, returning 0 can hide that distinction. Choose a return type and result that express the actual contract, such as an optional value or an explicit status. Do not add an arbitrary fallback merely to silence a diagnostic.
Debugging with Compiler Warnings
Enable warnings while compiling so the compiler can help locate a path to inspect. For example, g++ -Wall -Wextra -Wreturn-type your_code.cpp -o your_program asks GCC to report a range of warnings, including relevant return-path diagnostics where applicable. Warning availability and wording vary by compiler, version, options, and code; a warning is a clue to trace the control flow, not proof that every compiler treats the code the same way.
Inspect each branch, including nested if statements and every switch case. Then decide what the function should do for each input and return a value that honors that behavior. Keep warnings enabled during development so similar paths are easier to notice.
Conclusion
When a C++ non-void function can reach its closing brace without a value, trace the path and fix the function’s logic. Cover each case with a return that matches the function’s purpose. Use a fallback only when it has a defined meaning, and treat compiler warnings as help for finding paths to review.
FAQ
What causes the “control reaches end of non-void function” diagnostic?
A reachable path in a value-returning function reaches its closing brace without returning a value. Compilers can report this condition, though the diagnostic and build behavior depend on the compiler and options.
Does every if branch need a return?
No. A return after the conditional can handle the remaining path. What matters is that no reachable route falls through the end without a value.
Should I add return 0; to make the warning go away?
Only if zero is a valid result under the function’s contract. Otherwise, choose a result or return type that represents the missing case accurately.
Can a compiler warning identify the missing path?
It can point to a function that may fall through. Use it to inspect the control flow; warning text and behavior vary across compilers and settings.