A function is a subprogram that returns a value (the result of the function call). The specification of a function starts with the reserved word function, and the parameters, if any, must have the mode in (whether this mode is specified explicitly or implicitly). The statements of the function body (excluding statements of program units that are inner to the function body) must include one or more return statements specifying the returned value.
The exception PROGRAM_ERROR is raised if a function body is left otherwise than by a return statement. This does not apply if the execution of the function is abandoned as a result of an exception.
function DOT_PRODUCT(LEFT, RIGHT : VECTOR) return REAL is SUM : REAL := 0.0; begin CHECK(LEFT'FIRST = RIGHT'FIRST and LEFT'LAST = RIGHT'LAST); for J in LEFT'RANGE loop SUM := SUM + LEFT(J)*RIGHT(J); end loop; return SUM; end DOT_PRODUCT;
References: exception, formal parameter, function, function body, function call, function specification, mode, program_error exception, raising of exceptions, return statement, statement.
Rationale references: 8.4 Function Subprograms
Style Guide references: 2.1.1 Horizontal Spacing, 3.2.4 Program Unit Names, 4.1.3 Functions, 4.3.1 Using Exceptions to Help Define an Abstraction, 5.9.6 Initialization
Address any questions or comments to email@example.com.