[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
function                 Declares a Function (Subprogram Returning a Value)

 function  <fname>(<parameters>) : <ftype>;

   <declarations>

 begin
   <statements>
 end;

    A function is a subprogram: a set of declarations and statements that
    are executed whenever you refer to <fname>. (The other type of
    subprogram is a procedure.) A function returns a value of some scalar
    type: Integer, Real, Char, Boolean, or an enumerated type. You should
    always assign a value to <fname> before exiting the function. <fname>
    can only be used in expressions where a literal value could be used,
    that is, on the right side of an assignment statement, or as a pass-
    by-value parameter to a subprogram. A function may be recursive, that
    is, a reference to <fname> in the <statements> of the <fname>
    function.

            <fname>   The function's name.

       <parameters>   An optional list of parameters. If no parameters are
                      declared, then the parentheses are dropped.

            <ftype>   The type of value that the function returns:
                      Integer, Real, Char, Boolean, enumerated, or a
                      string-based type.

     <declarations>   Local LABEL, CONST, TYPE, VAR, PROCEDURE, and
                      FUNCTION declarations.

       <statements>   One or more legal statements. Should include the
                      assignment statement <fname> := <expr>, where <expr>
                      is an expression compatible with <ftype>.

  -------------------------------- Example ---------------------------------

    Below is a function that returns the larger of two integers:

           function Max(A,B : Integer) : Integer;
           begin
             if A > B
               then Max := A
               else Max := B
           end;

See Also: procedure program begin end
This page created by ng2html v1.05, the Norton guide to HTML conversion utility. Written by Dave Pearson