6.5. Function Subprograms


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;
       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.