This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
return Statement (C)Areturn statement ends the execution of a function, and returns control to the calling function. Execution resumes in the calling function at the point immediately following the call. Areturn statement can return a value to the calling function. For more information, seeReturn type.
jump-statement:
returnexpressionopt;
The value ofexpression, if present, is returned to the calling function. Ifexpression is omitted, the return value of the function is undefined. The expression, if present, is evaluated and then converted to the type returned by the function. When areturn statement contains an expression in functions that have avoid return type, the compiler generates a warning, and the expression isn't evaluated.
If noreturn statement appears in a function definition, control automatically returns to the calling function after the last statement of the called function is executed. In this case, the return value of the called function is undefined. If the function has a return type other thanvoid, it's a serious bug, and the compiler prints a warning diagnostic message. If the function has avoid return type, this behavior is okay, but may be considered poor style. Use a plainreturn statement to make your intent clear.
As a good engineering practice, always specify a return type for your functions. If a return value isn't required, declare the function to havevoid return type. If a return type isn't specified, the C compiler assumes a default return type ofint.
Many programmers use parentheses to enclose theexpression argument of thereturn statement. However, C doesn't require the parentheses.
The compiler may issue a warning diagnostic message about unreachable code if it finds any statements placed after thereturn statement.
In amain function, thereturn statement and expression are optional. What happens to the returned value, if one is specified, depends on the implementation.Microsoft-specific: The Microsoft C implementation returns the expression value to the process that invoked the program, such ascmd.exe. If noreturn expression is supplied, the Microsoft C runtime returns a value that indicates success (0) or failure (a non-zero value).
This example is one program in several parts. It demonstrates thereturn statement, and how it's used both to end function execution, and optionally, to return a value.
// C_return_statement.c// Compile using: cl /W4 C_return_statement.c#include <limits.h> // for INT_MAX#include <stdio.h> // for printflong long square( int value ){ // Cast one operand to long long to force the // expression to be evaluated as type long long. // Note that parentheses around the return expression // are allowed, but not required here. return ( value * (long long) value );}Thesquare function returns the square of its argument, in a wider type to prevent an arithmetic error.Microsoft-specific: In the Microsoft C implementation, thelong long type is large enough to hold the product of twoint values without overflow.
The parentheses around thereturn expression insquare are evaluated as part of the expression, and aren't required by thereturn statement.
double ratio( int numerator, int denominator ){ // Cast one operand to double to force floating-point // division. Otherwise, integer division is used, // then the result is converted to the return type. return numerator / (double) denominator;}Theratio function returns the ratio of its twoint arguments as a floating-pointdouble value. Thereturn expression is forced to use a floating-point operation by casting one of the operands todouble. Otherwise, the integer division operator would be used, and the fractional part would be lost.
void report_square( void ){ int value = INT_MAX; long long squared = 0LL; squared = square( value ); printf( "value = %d, squared = %lld\n", value, squared ); return; // Use an empty expression to return void.}Thereport_square function callssquare with a parameter value ofINT_MAX, the largest signed integer value that fits in anint. Thelong long result is stored insquared, then printed. Thereport_square function has avoid return type, so it doesn't have an expression in itsreturn statement.
void report_ratio( int top, int bottom ){ double fraction = ratio( top, bottom ); printf( "%d / %d = %.16f\n", top, bottom, fraction ); // It's okay to have no return statement for functions // that have void return types.}Thereport_ratio function callsratio with parameter values of1 andINT_MAX. Thedouble result is stored infraction, then printed. Thereport_ratio function has avoid return type, so it doesn't need to explicitly return a value. Execution ofreport_ratio "falls off the bottom" and returns no value to the caller.
int main(){ int n = 1; int x = INT_MAX; report_square(); report_ratio( n, x ); return 0;}Themain function calls two functions:report_square andreport_ratio. Asreport_square takes no parameters and returnsvoid, we don't assign its result to a variable. Likewise,report_ratio returnsvoid, so we don't save its return value, either. After each of these function calls, execution continues at the next statement. Thenmain returns a value of0 (typically used to report success) to end the program.
To compile the example, create a source code file namedC_return_statement.c. Then, copy all the example code, in the order shown. Save the file, and compile it in a Developer command prompt window by using the command:
cl /W4 C_return_statement.c
Then, to run the example code, enterC_return_statement.exe at the command prompt. The output of the example looks like this:
value = 2147483647, squared = 46116860141324206091 / 2147483647 = 0.0000000004656613Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?