6.5 Return Statements

From OC Systems Wiki!
Jump to: navigation, search

A return_statement is used to complete the execution of the innermost enclosing subprogram_body, entry_body, or accept_statement.

Syntax

return_statement ::= return [expression];

Name Resolution Rules

The expression, if any, of a return_statement is called the return expression. The result subtype of a function is the subtype denoted by the subtype_mark after the reserved word return in the profile of the function. The expected type for a return expression is the result type of the corresponding function.

Legality Rules

A return_statement shall be within a callable construct, and it applies to the innermost one. A return_statement shall not be within a body that is within the construct to which the return_statement applies.

A function body shall contain at least one return_statement that applies to the function body, unless the function contains code_statements. A return_statement shall include a return expression if and only if it applies to a function body.

Dynamic Semantics

For the execution of a return_statement, the expression (if any) is first evaluated and converted to the result subtype.

If the result type is class-wide, then the tag of the result is the tag of the value of the expression.

If the result type is a specific tagged type:

  • If it is limited, then a check is made that the tag of the value of the return expression identifies the result type. Constraint_Error is raised if this check fails.
  • If it is nonlimited, then the tag of the result is that of the result type.

A type is a return-by-reference type if it is a descendant of one of the following:

  • a tagged limited type;
  • a task or protected type;
  • a nonprivate type with the reserved word limited in its declaration;
  • a composite type with a subcomponent of a return-by-reference type;
  • a private type whose full type is a return-by-reference type.

If the result type is a return-by-reference type, then a check is made that the return expression is one of the following:

  • a name that denotes an object view whose accessibility level is not deeper than that of the master that elaborated the function body; or
  • a parenthesized expression or qualified_expression whose operand is one of these kinds of expressions.

The exception Program_Error is raised if this check fails.

For a function with a return-by-reference result type the result is returned by reference; that is, the function call denotes a constant view of the object associated with the value of the return expression. For any other function, the result is returned by copy; that is, the converted value is assigned into an anonymous constant created at the point of the return_statement, and the function call denotes that object.

Finally, a transfer of control is performed which completes the execution of the callable construct to which the return_statement applies, and returns to the caller.

Examples

Examples of return statements:

return;                         -- in a procedure body, entry_body, or accept_statement
return Key_Value(Last_Index);   -- in a function body

Copyright © 1992,1993,1994,1995 Intermetrics, Inc.
Copyright © 2000 The MITRE Corporation, Inc. Ada Reference Manual