[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
Functions                SubProgram

    Pascal allows you to create functions. A function is a type of
    subprogram and has a syntax nearly identical to that of programs, with
    two major (and one minor) exceptions. First, a function starts with a
    FUNCTION header, which takes the form:

         function <name><parameters> : <type>;

    where <name> is any legal identifier not previously declared,
    <parameters> is an optional list of parameters, enclosed in
    parentheses and separated by semicolons, and <type> is any ordinal
    type (Integer, Char, Boolean, or enumerated data type) or the type
    Real or String.

    Second, before exiting the function, you must assign a value of <type>
    to the function's name. The following function returns the maximum of
    two integer values:

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

    Finally, the main body of a function ends with a semicolon instead of
    a period.

    A function is executed by using its name, including any necessary
    parameters. For example, if you declare the function IMax as above,
    then you can call it in your program like this (assuming I, J, and K
    are integer variables):

         K := Max(I,J);

See Also: subprograms procedures parameters
This page created by ng2html v1.05, the Norton guide to HTML conversion utility. Written by Dave Pearson