Return statement
This article needs additional citations for verification. (February 2011) |
In
Overview
In
exp
. If a function has the return type 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.[1][2] Similar syntax is used in other languages including Modula-2[3] and Python.[4]In Pascal there is no return statement. Functions or procedures automatically return when reaching their last statement. The return value from a function is provided within the function by making an assignment to an identifier with the same name as the function.[5] However, some versions of Pascal provide a special function Exit(exp);
that can be used to return a value immediately from a function, or, without parameters, to return immediately from a procedure.[6]
Like Pascal,
In some other languages a user defined
Some expression-oriented programming language, such as Lisp, 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. In other cases a Null value is returned if there is no explicit return statement: in Python, the value None
is returned when the return statement is omitted,[4] while in JavaScript the value undefined
is returned.
In
In Perl, a return value or values of a subroutine can depend on the context in which it was called. The most fundamental distinction is a
wantarray
function. A special syntax of return without arguments is used to return an undefined value in scalar context and an empty list in list context. The scalar context can be further divided into Boolean, number, string, and various reference types contexts. Also, a context-sensitive object can be returned using a contextual return sequence, with lazy evaluationMany
Syntax
Return statements come in many shapes. The following syntaxes are most common:
Language | Return statement | If value omitted, return |
---|---|---|
Ada, Bourne shell,[a] C, C++, Java, PHP, C#, JavaScript, D | return value;
|
In the Bourne shell, exit value of the last command executed in the function
In C[1] and C++,[2] undefined behavior if function is value-returning In PHP,[12] returns In Javascript,[13] returns the value In Java and C#, not permitted if function is value-returning |
BASIC | RETURN
|
|
Lisp | (return value)
|
Last statement value |
Perl, Ruby | return @values;
return $value;
return;
or a contextual return sequence |
Last statement value |
PL/I |
return(expression); return; |
Undefined behavior if procedure is declared as returning a value |
Python | return value
|
None [4]
|
Smalltalk | ^ value
|
|
Tcl | return
return $value
return -code error "Error message"
or some more complicated combination of options |
Last statement value |
Visual Basic .NET
|
Return value
|
|
Windows PowerShell
|
return value;
|
Object |
x86 assembly
|
ret
|
Contents of eax register (by conventions) |
In some assembly languages, for example that for the MOS Technology 6502, the mnemonic "RTS" (ReTurn from Subroutine) is used.
Multiple return statements
Languages with an explicit return statement create the possibility of multiple return statements in the same function. Whether or not that is a good thing is controversial.
Strong adherents of
In his 2004 textbook,
According to empirical studies cited by Eric S. Roberts, student programmers had difficulty formulating correct solutions for several simple problems in a language like Pascal, which does not allow multiple exit points. For the problem of writing a function to linearly searching an element in an array, a 1980 study by Henry Shapiro (cited by Roberts) found that using only the Pascal-provided control structures, the correct solution was given by only 20% of the subjects, while no subject wrote incorrect code for this problem if allowed to write a return from the middle of a loop.[16]
Others, including Kent Beck and Martin Fowler argue that one or more guard clauses—conditional "early exit" return statements near the beginning of a function—often make a function easier to read than the alternative.[17][18][19][20]
The most common problem in early exit is that cleanup or final statements are not executed – for example, allocated memory is not unallocated, or open files are not closed, causing leaks. These must be done at each return site, which is brittle and can easily result in bugs. For instance, 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 (e.g. a trace statement) might not be performed in all cases. Languages without a return statement, such as standard Pascal don't have this problem. Some languages, such as C++ and Python, employ concepts which allow actions to be performed automatically upon return (or exception throw) which mitigates some of these issues – these are often known as "try/finally" or similar. Functionality like these "finally" clauses can be implemented by a goto to the single return point of the subroutine. An alternative solution is to use the normal stack unwinding (variable deallocation) at function exit to unallocate resources, such as via destructors on local variables, or similar mechanisms such as Python's "with" statement.
Some early implementations of languages such as the original Pascal and C restricted the types that can be returned by a function (e.g. not supporting record or struct types) to simplify their compilers.
In
Yield statements
Cousin to return statements are
Call/return sequences
A number of possible call/return sequences are possible depending on the hardware instruction set, including the following:
- The
CALL
instruction pushes address of the next instruction on the stack and branches to the specified address. TheRETURN
instruction pops the return address from the stack into the instruction pointer and execution resumes at that address. (Examples: x86, PDP-11) In architectures such as the Motorola 96000, the stack area may be allocated in a separate address space, which is called 'Stack Memory Space',[22] distinct from the main memory address space.[23] The NEC μPD7720 also features a stack with its own separate address space.[24] - The
CALL
instruction places address of the next instruction in a register and branches to the specified address. TheRETURN
instruction sequence places the return address from the register into the instruction pointer and execution resumes at that address. (Examples:RISCarchitectures) - The
CALL
instruction places address of the next (or current) instruction in the storage location at the call address and branches to the specified address+1. TheRETURN
instruction sequence branches to the return address by an indirect jump to the first instruction of the subroutine. (Examples: IBM 1130, SDS 9XX, PDP-8)
See also
Notes
References
- ^ a b "return Statement (C)". Microsoft Docs. 25 January 2023.
- ^ a b "return Statement (C++)". Microsoft Docs. 3 August 2021.
- ISBN 9781461385318.
- ^ ISBN 9781449379100.
- ^ ISBN 9780126339512.
- ISBN 9781461224280.
- ^ ANSI x3.9-1966. USA Standard FORTRAN (PDF). American National Standards Institute. p. 14. Archived from the original (PDF) on May 15, 2011. Retrieved May 5, 2010.
{{cite book}}
: CS1 maint: numeric names: authors list (link) - ^ ANSI x3.9-1978. American National Standard – Programming Language FORTRAN. American National Standards Institute. 15.8 RETURN Statement. Archived from the original on October 29, 2013. Retrieved December 11, 2007.
{{cite book}}
: CS1 maint: numeric names: authors list (link) - .
- ^ Wirth, Niklaus (May 3, 2016). "10. Procedure declarations". The Programming Language Oberon (PDF) (Report). p. 11.
- ^ "return - return from a function or dot script". Single UNIX Specification.
- ^ "PHP: return - Manual". PHP Manual. The PHP Group. Retrieved 26 March 2013.
- ^ "Return - Javascript". MDN Javascript Reference. Mozilla Developer Network. Retrieved 27 March 2013.
- ^ "C++ Notes: Function return Statement".
- ISBN 978-0-470-85320-7.
- .
- ISBN 9780133065268.
... one exit point mentality ... I don't follow the rule about one exit point from a method.
- ^
Kent Beck (2007). "7: Behavior". Implementation Patterns. Pearson Education. section "Guard Clause". ISBN 9780132702553.
- ^ "Multiple return statements". Java Practices.
- ^ Fred Swartz. "Return statements and the single exit fantasy". Archived from the original on 2020-02-23.
- ^ "The finally Block". The Java Tutorials.
- ^ "DSP96002 32-BIT DIGITAL SIGNAL PROCESSOR USER'S MANUAL" (PDF). p. 27(3 - 4). Retrieved 2023-12-24.
- ^ "DSP96002 32-BIT DIGITAL SIGNAL PROCESSOR USER'S MANUAL" (PDF). p. 50(4 - 11). Retrieved 2023-12-24.
- ^ "μPD77C20A, 7720A, 77P20 Digital Signal Processor". p. 4(3a-4). Retrieved 2023-12-25.