5.8. Return Statements

[PREVIOUS][UP][NEXT]

A return statement is used to complete the execution of the innermost enclosing function, procedure, or accept statement.

    return_statement ::= return [expression]; 

A return statement is only allowed within the body of a subprogram or generic subprogram, or within an accept statement, and applies to the innermost (enclosing) such construct; a return statement is not allowed within the body of a task unit, package, or generic package enclosed by this construct (on the other hand, it is allowed within a compound statement enclosed by this construct and, in particular, in a block statement).

A return statement for an accept statement or for the body of a procedure or generic procedure must not include an expression. A return statement for the body of a function or generic function must include an expression.

The value of the expression defines the result returned by the function. The type of this expression must be the base type of the type mark given after the reserved word return in the specification of the function or generic function (this type mark defines the result subtype).

For the execution of a return statement, the expression (if any) is first evaluated and a check is made that the value belongs to the result subtype. The execution of the return statement is thereby completed if the check succeeds; so also is the execution of the subprogram or of the accept statement. The exception CONSTRAINT_ERROR is raised at the place of the return statement if the check fails.

Examples:

    return;                         -- in a procedure
    return KEY_VALUE(LAST_INDEX);   -- in a function

Note:

If the expression is either a numeric literal or named number, or an attribute that yields a result of type universal_integer or universal_real, then an implicit conversion of the result is performed as described in section 4.6.

References: attribute, block statement, constraint_error exception, expression, function body, function call, generic body, implicit type conversion, named number, numeric literal, package body, procedure body, reserved word, result subtype, subprogram body, subprogram specification, subtype, task body, type mark, universal_integer type, universal_real type.


[INDEX][CONTENTS]