(Redirected from Return value)
In
computer programming, a 'return statement' causes execution to leave the current
subroutine and resume at the point the subroutine was called -- known as its
return address. The return address is saved, usually on the
process's call stack, as part of the operation of making the subroutine call. 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 statement 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
Pascal there is no return statement. A subroutine automatically returns when execution reaches its last executable statement. Values may be returned by assigning to an identifier that has the same name as the subroutine (a ''function'' in Pascal terminology).
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.
Syntax
Return statements come in many shapes. The following syntaxes are most common:
As used in
C,
C++,
Java,
PHP,
C#,
Windows PowerShell:
return ''value'';
As used in
Smalltalk:
^ ''value''
As used in
Lisp:
(return ''value'')
As used in
BASIC:
RETURN
Criticism
Some developers and computer programming educators eschew use of the explicit return statement except at the textual end of a subroutine - considering that, when it is used to "return early", it suffers from the same sort of readability problems as the
GOTO statement. As with GOTO, there is a problem that in later development, a return statement could be overlooked by a developer, and an action which should be performed at the end of a subroutine might not be performed (in all cases).
However, it is generally accepted that the return statement should not be
considered as harmful as GOTO, since it only allows jumping to one particular place in the subroutine - and as a result, tends to be used less frequently. Moreover, the return value of a function is usually (in popular contemporary programming languages) explicitly specified in the return statement.
Also, in languages such as
Java, the ''try..finally'' construct can be used to always perform an action at the very end of a subroutine, after any return instruction has been executed. The finally clause will be executed even if an
exception has been thrown (the exception is automatically caught, and then re-thrown after the finally clause has been executed, unless the finally clause ''itself'' throws an exception). When used appropriately (e.g. for closing file handles), this avoids the need to either find all exit points and add redundant statements immediately before them, or to refactor the subroutine to remove return statements - both of which are potentially error-prone procedures.
Aspect-oriented programming can also be used to do a similar job to ''try..finally'' - with or without the automatic exception-catching behaviour - although it is intended more to concisely express behaviours which "crosscut" more than one subroutine.