6.5. Function Subprograms

[PREVIOUS][UP][NEXT]

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.

Example:

    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.


[INDEX][CONTENTS]