This article needs additional citations for verification .(February 2011) |
In computer programming, a return statement causes execution to leave the current subroutine and resume at the point in the code immediately after the instruction which called the subroutine, known as its return address. The return address is saved by the calling routine, today usually on the process's call stack or in a register. Return statements in many programming languages allow a function to specify a return value to be passed back to the code that called the function.
In C and 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 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, FORTRAN II, Fortran 66, Fortran 77, and later versions of Fortran specify return values by an assignment to the function name, but also have a return statement; that statement does not specify a return value and, for a function, causes the value assigned to the function name to be returned. [5] [7] [8]
In some other languages a user defined output parameter is used instead of the function identifier. [9]
Oberon (Oberon-07) has a return clause instead of a return statement. The return clause is placed after the last statement of the procedure body. [10]
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 Windows PowerShell all evaluated expressions which are not captured (e.g., assigned to a variable, cast to void or piped to $null) are returned from the subroutine as elements in an array, or as a single object in the case that only one object has not been captured.
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 scalar context where the calling code expects one value, a list context where the calling code expects a list of values and a void context where the calling code doesn't expect any return value at all. A subroutine can check the context using the 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 evaluation of scalar values.
Many operating systems let a program return a result (separate from normal output) when its process terminates; these values are referred to exit statuses. The amount of information that can be passed this way is quite limited, in practice often restricted to signalling success or fail. From within the program this return is typically achieved by calling Exit (system call) (common even in C, where the alternative mechanism of returning from the main function is available).
Return statements come in many shapes. The following syntaxes are most common:
Language | Return statement | If value omitted, return |
---|---|---|
Ada, Bourne shell, [lower-alpha 1] C, C++, Java, PHP, C#, JavaScript, D | returnvalue; | 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 | (returnvalue) | 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 | returnvalue | None [4] |
Smalltalk | ^value | |
Tcl | returnreturn$valuereturn-codeerror"Error message" or some more complicated combination of options | Last statement value |
Visual Basic .NET | Returnvalue | |
Windows PowerShell | returnvalue; | 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.
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 structured programming make sure each function has a single entry and a single exit (SESE). It has thus been argued [14] that one should eschew the use of the explicit return statement except at the textual end of a subroutine, considering that, when it is used to "return early", it may suffer from the same sort of problems that arise for the GOTO statement. Conversely, it can be argued that using the return statement is worthwhile when the alternative is more convoluted code, such as deeper nesting, harming readability.
In his 2004 textbook, David Watt writes that "single-entry multi-exit control flows are often desirable". Using Tennent's framework notion of sequencer, Watt uniformly describes the control flow constructs found in contemporary programming languages and attempts to explain why certain types of sequencers are preferable to others in the context of multi-exit control flows. Watt writes that unrestricted gotos (jump sequencers) are bad because the destination of the jump is not self-explanatory to the reader of a program until the reader finds and examines the actual label or address that is the target of the jump. In contrast, Watt argues that the conceptual intent of a return sequencer is clear from its own context, without having to examine its destination. Furthermore, Watt writes that a class of sequencers known as escape sequencers, defined as "sequencer that terminates execution of a textually enclosing command or procedure", encompasses both breaks from loops (including multi-level breaks) and return statements. Watt also notes that while jump sequencers (gotos) have been somewhat restricted in languages like C, where the target must be an inside the local block or an encompassing outer block, that restriction alone is not sufficient to make the intent of gotos in C self-describing and so they can still produce "spaghetti code". Watt also examines how exception sequencers differ from escape and jump sequencers; for details on this see the article on structured programming. [15]
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 Java—and similar languages modeled after it, like JavaScript—it is possible to execute code even after return statement, because the finally block of a try-catch structure is always executed. So if the return statement is placed somewhere within try or catch blocks the code within finally (if added) will be executed. It is even possible to alter the return value of a non primitive type (a property of an already returned object) because the exit occurs afterwards as well. [21]
Cousin to return statements are yield statements: where a return causes a subroutine to terminate, a yield causes a coroutine to suspend. The coroutine will later continue from where it suspended if it is called again. Coroutines are significantly more involved to implement than subroutines, and thus yield statements are less common than return statements, but they are found in a number of languages.
A number of possible call/return sequences are possible depending on the hardware instruction set, including the following:
CALL
instruction pushes address of the next instruction on the stack and branches to the specified address. The RETURN
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] CALL
instruction places address of the next instruction in a register and branches to the specified address. The RETURN
instruction sequence places the return address from the register into the instruction pointer and execution resumes at that address. (Examples: IBM System/360 and successors through z/Architecture, most RISC architectures)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. The RETURN
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)Procedural programming is a programming paradigm, derived from imperative programming, based on the concept of the procedure call. Procedures simply contain a series of computational steps to be carried out. Any given procedure might be called at any point during a program's execution, including by other procedures or itself. The first major procedural programming languages appeared c. 1957–1964, including Fortran, ALGOL, COBOL, PL/I and BASIC. Pascal and C were published c. 1970–1972.
Structured programming is a programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of the structured control flow constructs of selection (if/then/else) and repetition, block structures, and subroutines.
In computer science, control flow is the order in which individual statements, instructions or function calls of an imperative program are executed or evaluated. The emphasis on explicit control flow distinguishes an imperative programming language from a declarative programming language.
In computer science, imperative programming is a programming paradigm of software that uses statements that change a program's state. In much the same way that the imperative mood in natural languages expresses commands, an imperative program consists of commands for the computer to perform. Imperative programming focuses on describing how a program operates step by step, rather than on high-level descriptions of its expected results.
Coroutines are computer program components that allow execution to be suspended and resumed, generalizing subroutines for cooperative multitasking. Coroutines are well-suited for implementing familiar program components such as cooperative tasks, exceptions, event loops, iterators, infinite lists and pipes.
In computer programming, a parameter or a formal argument is a special kind of variable used in a subroutine to refer to one of the pieces of data provided as input to the subroutine. These pieces of data are the values of the arguments with which the subroutine is going to be called/invoked. An ordered list of parameters is usually included in the definition of a subroutine, so that, each time the subroutine is called, its arguments for that call are evaluated, and the resulting values can be assigned to the corresponding parameters.
In computer programming, a block or code block or block of code is a lexical structure of source code which is grouped together. Blocks consist of one or more declarations and statements. A programming language that permits the creation of blocks, including blocks nested within other blocks, is called a block-structured programming language. Blocks are fundamental to structured programming, where control structures are formed from blocks.
In computer science, a NOP, no-op, or NOOP is a machine language instruction and its assembly language mnemonic, programming language statement, or computer protocol command that does nothing.
In computer science, conditionals are programming language commands for handling decisions. Specifically, conditionals perform different computations or actions depending on whether a programmer-defined Boolean condition evaluates to true or false. In terms of control flow, the decision is always achieved by selectively altering the control flow based on some condition . Although dynamic dispatch is not usually classified as a conditional construct, it is another way to select between alternatives at runtime. Conditional statements are the checkpoints in the programe that determines behaviour according to situation.
In software development, a breakpoint is an intentional stopping or pausing place in a program, put in place for debugging purposes. It is also sometimes simply referred to as a pause.
In computer programming, a statement is a syntactic unit of an imperative programming language that expresses some action to be carried out. A program written in such a language is formed by a sequence of one or more statements. A statement may have internal components.
A branch is an instruction in a computer program that can cause a computer to begin executing a different instruction sequence and thus deviate from its default behavior of executing instructions in order. Branch may also refer to the act of switching execution to a different instruction sequence as a result of executing a branch instruction. Branch instructions are used to implement control flow in program loops and conditionals.
In computer science, a tail call is a subroutine call performed as the final action of a procedure. If the target of a tail is the same subroutine, the subroutine is said to be tail recursive, which is a special case of direct recursion. Tail recursion is particularly useful, and is often easy to optimize in implementations.
In computer science, a call stack is a stack data structure that stores information about the active subroutines of a computer program. This type of stack is also known as an execution stack, program stack, control stack, run-time stack, or machine stack, and is often shortened to simply "the stack". Although maintenance of the call stack is important for the proper functioning of most software, the details are normally hidden and automatic in high-level programming languages. Many computer instruction sets provide special instructions for manipulating stacks.
In computer science, a calling convention is an implementation-level (low-level) scheme for how subroutines or functions receive parameters from their caller and how they return a result. When some code calls a function, design choices have been taken for where and how parameters are passed to that function, and where and how results are returned from that function, with these transfers typically done via certain registers or within a stack frame on the call stack. There are design choices for how the tasks of preparing for a function call and restoring the environment after the function has completed are divided between the caller and the callee. Some calling convention specifies the way every function should get called. The correct calling convention should be used for every function call, to allow the correct and reliable execution of the whole program using these functions.
In computer programming languages, a switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
setjmp.h is a header defined in the C standard library to provide "non-local jumps": control flow that deviates from the usual subroutine call and return sequence. The complementary functions setjmp
and longjmp
provide this functionality.
Goto is a statement found in many computer programming languages. It performs a one-way transfer of control to another line of code; in contrast a function call normally returns control. The jumped-to locations are usually identified using labels, though some languages use line numbers. At the machine code level, a goto
is a form of branch or jump statement, in some cases combined with a stack adjustment. Many languages support the goto
statement, and many do not.
In computer programming, a function or subroutine is a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed.
The OS/360 Object File Format is the standard object module file format for the IBM DOS/360, OS/360 and VM/370, Univac VS/9, and Fujitsu BS2000 mainframe operating systems. In the 1990s, the format was given an extension with the XSD-type record for the MVS Operating System to support longer module names in the C Programming Language. This format is still in use by the z/VSE operating system. In contrast, it has been superseded by the GOFF file format on the MVS Operating System and on the z/VM Operating System. Since the MVS and z/VM loaders will still handle this older format, some compilers have chosen to continue to produce this format instead of the newer GOFF format.
{{cite book}}
: CS1 maint: numeric names: authors list (link){{cite book}}
: CS1 maint: numeric names: authors list (link)... one exit point mentality ... I don't follow the rule about one exit point from a method.