Return statement
From Wikipedia, the free encyclopedia
In computer programming, a return statement causes execution to leave the current subroutine and resume at the point the subroutine was called -- this is called the return address. The program knows where to return to by examining the top of the process's call stack. Return statements in many languages allow a function to specify a return value to be passed back to the code that called the function.
In C++, return exp;
(where exp
is an expression) is a statement that tells a function to return execution of the program to the calling function, and report the value of exp
. If a function does not have a return type (i.e., its return type is void), the return command can be used without a value, in which case the program just breaks out of the current function and returns to the calling one. In some languages, such as Pascal and most versions of BASIC, that is the only style of return statement; a return value is specified in a separate statement.
Certain programming languages, such as Perl and Ruby allow the programmer to omit an explicit return statement, specifying instead that the last evaluated expression is the return value of the subroutine. Values returned by the program when it terminates are often captured by batch programs.
[edit] Syntax
Return statements come in many shapes. The following syntaxes are most common:
As used in Java, C#, C, C++, and PHP:
return value;
As used in Smalltalk:
^ value
As used in Lisp:
(return value)
As used in BASIC:
RETURN