Contents Index Search Previous Next
6.5 Return Statements
1
A return_statement
is used to complete the execution of the innermost enclosing subprogram_body,
entry_body, or accept_statement.
Syntax
2
return_statement
::= return [
expression];
Name Resolution Rules
3
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
4
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.
5
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
6
For the execution of a
return_statement,
the
expression (if any) is first
evaluated and converted to the result subtype.
7
If the result type is class-wide, then the tag
of the result is the tag of the value of the expression.
8
If the result type
is a specific tagged type:
9
- 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.
10
- If it is nonlimited, then the tag
of the result is that of the result type.
11
A
type is a
return-by-reference type if it is a descendant of one
of the following:
12
13
- a task or protected type;
14
- a nonprivate type with the reserved
word limited in its declaration;
15
- a composite type with a subcomponent
of a return-by-reference type;
16
- a private type whose full type is
a return-by-reference type.
17
If
the result type is a return-by-reference type, then a check is made that
the return expression is one of the following:
18
- a name
that denotes an object view whose accessibility level is not deeper than
that of the master that elaborated the function body; or
19
- a parenthesized expression or qualified_expression
whose operand is one of these kinds of expressions.
20
The exception Program_Error
is raised if this check fails.
21
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.
22
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
23
Examples of
return statements:
24
return; -- in a procedure body, entry_body, or accept_statement
return Key_Value(Last_Index); -- in a function body
Contents Index Search Previous Next Legal