# V591. Non-void function must return value.

The analyzer has detected a non-void function with an execution path that does not return a value. Such a function results in undefined behavior.

Flowing off the end of a non-void function with no 'return' results in undefined behavior.

Let's consider an example:

    int GetSign(int arg)
    {
      if (arg > 0)
      {
        return 1;
      }
      else if (arg < 0)
      {
        return -1;
      }
    }

If the 'GetSign' function receives 0, undefined behavior will occur. Here's the correct version:

    int GetSign(int arg)
    {
      if (arg > 0)
      {
        return 1;
      }
      else if (arg < 0)
      {
        return -1;
      }

      return 0;
    }

The 'main' and 'wmain' functions are the exceptions. Flowing off the end of these functions is equivalent to a 'return 0;'. Thus, these functions do not result in undefined behavior. Let's consider an example.

    ....
    int main()
    {
      AnalyzeFile(FILE_NAME);
    }

In this case, we have the 'main' function. There will be no undefined behavior here. That's why, the analyzer will not issue a warning. The code fragment is equivalent to the following:

    ....
    int main()
    {
      AnalyzeFile(FILE_NAME);
      return 0;
    }

Note that undefined behavior occurs only if the end of a non-void function is actually reached. Particularly, if during the function execution an exception is thrown and is not caught in the body of the same function, there will be no undefined behavior.

The analyzer will not issue a warning for the following code fragment:

    int Calc(int arg);

    int Bar(int arg)
    {
      if (arg > 0)
      {
        return Calc(arg);
      }
      throw std::logic_error { "bad arg was passed to Bar" };
    }

There will also be no undefined behavior if, during the function execution, another function that does not return control, is called. Such functions are usually marked '[[noreturn]]'. Thus, the analyzer will not issue a warning for the following code fragment:

    [[noreturn]] void exit(int exit_code);

    int Foo()
    {
      ....
      exit(10);
    }

This diagnostic is classified as:

CWE-393 CERT-MSC52-CPP

You can look at examples of errors detected by the V591 diagnostic.