INFO: C and C++ Differences Regarding the Return Statement

Last reviewed: September 4, 1997
Article ID: Q85477
The information in this article applies to:
  • The Microsoft C/C++ Compiler (CL.EXE) included with: - Microsoft C/C++ for MS-DOS, version 7.0 - Microsoft Visual C++ for Windows, versions 1.0, 1.5, 1.51, 1.52 - Microsoft Visual C++, 32-bit Edition, versions 1.0, 2.0, 2.1, 4.0, 5.0

SUMMARY

With both C and C++, the return statement terminates the function currently executing and returns control back to its caller. However, the C and C++ languages differ slightly in their requirement that all paths of control through a function return a value of the correct type.

MORE INFORMATION

The return statement has the form:

   return [expression];

The expression portion of the statement is optional. If an expression is not specified, the return statement becomes equivalent to:

   return void;

Also, reaching the } (right curly brace) that terminates a function is equivalent to executing a return statement without an expression.

The C++ language requires that all possible control paths through a function return a value that is the same type as the return type specified by the function prototype. If this is not the case, a fatal error is generated by the compiler. The C language is not as strict in this requirement. In C, if a control path ends in a return statement that returns a value that is of a different type than that specified in the function prototype, a warning may be generated but compilation will continue.

Section r.6.6.3 of the C++ reference manual provided as a subsection of the second edition of "The C++ Programming Language" by Bjarne Stroustrup states:

   A return statement without an expression can be used only in
   functions that do not return a value, that is, a function with the
   return value type void, a constructor, or a destructor. A return
   statement with an expression can be used only in functions
   returning a value; the value of the expression is returned to the
   caller of the function. ... Flowing off the end of a function is
   equivalent to a return with no value; this is illegal in a value-
   returning function.

However, Section 3.6.6.4 of the ANSI specification for the C programming language states the following:

   If a return statement without an expression is executed, and the
   value of the function call is used by the caller, the behavior is
   undefined.

If the sample code given below is compiled as a C++ file (.CPP), the following error is generated by the compiler:

   error C2202: 'Func' : not all control paths return a value

However, if the same code is compiled as a C file (.C), compilation completes without any warnings or errors.

Sample Code

   /* Compile options needed: /c /W4
   */

   int Func ( int );

   int Func ( int nParam )
   {
        if ( nParam )
             return 1;
        else
             ;
   }

The C++ compiler generates the C2202 error because it is possible for the execution path of the function Func to flow off the end of the function if nParam is equal to 0 (zero). Because encountering the } is equivalent to a return statement with no expression, or a return type of void, the compiler generates the error because this type does not match the return type of int specified in the function prototype.

However, the C compiler sees that there is a return statement present that does return the correct type. Because of this, the compiler does not issue a warning. If no return statement is present at all, the C compiler generates the warning:

   warning C4035: 'Func' : no return value

Compilation completes but any function that relies on the return value from the function Func will be reading possibly invalid data.

With Microsoft C++, the only exception to the requirement that every execution path in a C++ file return a value of the correct type is the main() function.

If the sample code given below is compiled as a C++ file (.CPP), the following warning is generated by the compiler:

   warning C4508: 'main' : function should return a value; 'void'
                           return type assumed

If the same code is compiled as a C file (.C), the following warning is generated:

   warning C4035: 'main' : no return value

In both cases, compilation will complete.

Sample Code

   /* Compile options needed: /c /W3
   */

   int main ( void );

   int main ( void )
   {
   }


Additional query words: 8.00 8.00c 9.00 9.10
Keywords : CPPIss kbcode
Version : MS-DOS:7.0; WINDOWS:1.0,1.5,1.51,1.52; WINDOWS NT:1.0,2.0,2.1,4.0,5.0
Platform : MS-DOS NT WINDOWS
Issue type : kberrmsg kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 4, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.