Ad Space — Top Banner

C4715

Visual C++ Programming Language

Severity: Minor

What Does This Error Mean?

C4715 is a warning that a non-void function has at least one path through its body that reaches the end without executing a return statement. If that path is taken at runtime, the function returns garbage — undefined behavior. The fix is to add a return statement to every branch or add a default return value at the end of the function.

Affected Models

  • Visual Studio 2015–2022
  • MSVC v14.x / v17.x

Common Causes

  • An if/else chain does not cover all cases and the function falls off the end
  • A switch statement is missing a default case with a return
  • An early return inside an if block is the only return — the else path falls through
  • Exception-only exit paths that the compiler cannot verify cover all cases

How to Fix It

  1. Identify the branch or path that exits without returning. Look at every if, else, and switch arm and verify that each one ends with a return or throws an exception.

    The compiler message includes the function name. In Visual Studio, the green squiggly underline on the closing brace of the function marks the problem.

  2. Add a default return at the end of the function as a safety net. Even if you are certain all branches are covered, a final return prevents the warning and makes the intent clear.

    For functions returning bool, add return false at the end. For int functions, add return 0 or return -1 depending on what makes sense for the error case.

  3. For switch statements, add a default case that returns a value or throws an exception. This covers any enum values added in the future.

    A default that throws std::logic_error or calls abort() is a valid way to document that the path should never be reached. The compiler will accept it and stop producing C4715.

Frequently Asked Questions

Is C4715 a real bug or just a style warning?

It can be a real bug. If the missing return path is actually reached, the function returns whatever garbage value happens to be in the return register — a silent bug that can corrupt data or crash later. Treat C4715 as an error: every non-void function must explicitly return a value on every path.