# C/C++: warning: control reaches end of non-void function [-Wreturn-type]

**Code:** `control reaches end of non-void function` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang

A function that promises a return value can finish without returning one on some path — undefined behaviour in C++.

## 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 if` chain without a final `else` return
- A `switch` without a default that returns
- Return placed only inside a loop that might not run
- Function body missing `return` after a catch block

## Quick fix

1. Add a return or throw at the end of the function
2. Treat the warning as an error with `-Werror=return-type`
3. Add `default:` to switch statements

## Detailed fix by platform

### GCC / Clang

1. Enable it explicitly: `g++ -Wall -Wextra -Werror=return-type main.cpp`
2. Add `[[noreturn]]` to functions that never return (for example wrappers around `abort`)

## Code examples

### Missing return on one path

```cpp
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

1. **Paths** — Which input reaches the end?
2. **Switch** — Is there a default?
3. **Flags** — Compiled with -Wall?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/control-reaches-end-of-non-void)
