Overview

A return statement is a control construct in many programming languages that specifies the value produced by a function, method, or procedure and transfers control back to its caller. In object-oriented contexts a method may use a return to provide an output to the code that invoked it. The act of returning both supplies data and ends—or sometimes alters—the normal flow of execution within that subroutine, so callers receive results and can continue processing.

Forms and behavior

Return behaviour differs by language and by the subroutine's declared return type. Common patterns include:

  • Typed returns: Languages such as statically typed languages require a declared return type (for example a numeric type or an object). The returned value must match or be convertible to that type.
  • Void or unit: Some routines return no meaningful value; they use a special type (often called void or unit) and the return serves only to exit the routine.
  • Multiple values: Several languages allow returning tuples or multiple values, which callers can destructure.
  • Implicit returns: Expression-oriented languages or some scripting languages may return the last evaluated expression without an explicit return keyword.

History and language differences

The concept of returning a value dates back to early procedural languages where functions produced results. Over time languages introduced variations: explicit return statements, expression returns, and distinct handling for methods on objects. Different communities document these choices; for introductory material about general programming concepts see programming basics, for object-oriented method conventions see object-oriented programming, and for terminology about objects and methods see objects and methods.

Practical uses and examples

Returns are used to supply computation results, indicate success or failure, and pass data between layers of a program. Common idioms include early returns to handle error conditions and guard clauses that keep the main logic at a single indentation level. Returning immutable values and avoiding side effects are recommended in many designs to make functions easier to test and reason about.

Notable distinctions and caveats

Returns are different from exceptions: a returned value is normal output, while exceptions signal abnormal conditions and unwind the call stack. Optimizations such as tail-call elimination can affect how a return behaves at runtime in some compilers or interpreters, but this is language- and implementation-dependent. Understanding whether a language treats return as a statement or an expression is important for correct idiomatic use.

In short, the return statement is a fundamental mechanism for producing results and controlling flow; its exact syntax and semantics vary, so consult language documentation for details and idiomatic practices.