INFO: C and C++ Differences Regarding the Return StatementLast reviewed: September 4, 1997Article ID: Q85477 |
The information in this article applies to:
SUMMARYWith 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 INFORMATIONThe 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 valueHowever, 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 valueCompilation 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 assumedIf the same code is compiled as a C file (.C), the following warning is generated:
warning C4035: 'main' : no return valueIn 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
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |