return

Terminates execution of a function and returns control to the point immediately following the function call, where upon execution continues.

 

In the case of the main function, control is transferred back to the operating system.

 

Syntax:

return [expression] ;

 

The value of the expression is returned to the calling function.

 

If there is no expression the return value is said to be undefined.

 

Void functions do not and cannot have a return expression. You may also sometimes see a void function with no return statement, thus being undefined.

 

All other types of function must specify a return expression.

 

Compile & Run:

42 is larger than 17

 

 

You will often see return 0 ;  just before the end of main(). This is known as the application exit status, which is returning a value of 0 to the operating system. Standard convention has main() being declared with int preceding it, hence the ability to return a number to the OS, which can potentially be used by other programs or the OS as necessary. For instance, if an application exited with a return value of 1, this might alert the OS or other application that something went wrong, and can be acted upon accordingly.

Leave a Reply