C/C++: warning: control reaches end of non-void function [-Wreturn-type]
A function that promises a return value can finish without returning one on some path — undefined behaviour in C++.
control reaches end of non-void functionwarning: no return statement in function returning non-voidnon-void function does not return a value in all control pathsmissing return statement Meaning
Flowing off the end of a non-void function is undefined behaviour in C++ (the caller may use garbage, or execution may fall into the next function). It often shows up as a crash only at -O2. Return on every path, or restructure with a final return / throw.
Common causes
- An
if/else ifchain without a finalelsereturn - A
switchwithout a default that returns - Return placed only inside a loop that might not run
- Function body missing
returnafter a catch block
⚡ Quick fix
- Add a return or throw at the end of the function
- Treat the warning as an error with
-Werror=return-type - Add
default:to switch statements
Detailed fix by platform
GCC / Clang
- Enable it explicitly:
g++ -Wall -Wextra -Werror=return-type main.cpp - Add
[[noreturn]]to functions that never return (for example wrappers aroundabort)
Code examples
Missing return on one path
int sign(int x) {
if (x > 0) return 1;
if (x < 0) return -1;
// warning: control reaches end of non-void function (x == 0)
return 0; // fix
}How to diagnose
- Paths — Which input reaches the end?
- Switch — Is there a default?
- Flags — Compiled with -Wall?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- C4996 MSVC: error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead
- cannot declare variable to be of abstract type C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions)
- comparison of integers of different signs C/C++: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' [-Wsign-compare]
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026