The return Statement
Collapse the table of content
Expand the table of content
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

The return Statement

Terminates the execution of a function and returns control to the calling function (or, in the case of the main function, transfers control back to the operating system). Execution resumes in the calling function at the point immediately following the call.

return [expression]

The value of expression, if present, is returned to the calling function. If expression is omitted, the return value of the function is undefined. Functions of type void, constructors, and destructors cannot specify expressions in the return statement; functions of all other types must specify an expression in the return statement.

The expression, if specified, is converted to the type specified in the function declaration, as if an initialization were being performed. Conversion from the type of the expression to the return type of the function can cause temporary objects to be created. See Temporary Objects for more information about how and when temporaries are created.

When the flow of control exits the block enclosing the function definition, the result is the same as it would be if a return statement with no expression had been executed. This is illegal for functions that are declared as returning a value.

A function can have any number of return statements.

The following example uses an expression with return to obtain the largest of two integers.

// return_statement2.cpp
#include <stdio.h>

int max ( int a, int b )
   return ( a > b ? a : b );

int main()
    int nOne = 5;
    int nTwo = 7;

    printf_s("\n%d is bigger\n", max( nOne, nTwo ));
© 2016 Microsoft