Answer to Question #24394 in C++ for Prankur Verma

Question #24394
Why do we use return keyword, and how is it& beneficial for a program?
Expert's answer
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 ));

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!


No comments. Be first!

Leave a comment

Ask Your question

New on Blog