[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

#### 8.2.10.9 Procedural and Functional Types

For procedures without a parameter list:

    procedure type identifier = procedure name identifier;

or functions:

    function type identifier =
           function name identifier: function result type;

For procedures with a parameter list:

    procedure type identifier =
           procedure name identifier (parameter list);

or functions:

    function type identifier =
           function name identifier (parameter list): function result type;

Procedural types can be used as procedures or functions respectively, but also a value can be assigned to them. Procedural types are a Borland Pascal extension. In Borland Pascal, function result type can only be one of these types: ordinal types, enumerated types, real types, pointer types, the intrinsic Boolean, or the intrinsic String. In GNU Pascal every function result type for procedural types is allowed.

BP has procedural and functional types:

    type
      CompareFunction = function (Key1, Key2: String): Integer;

    function Sort (Compare: CompareFunction);
    begin
      ...
    end;

Standard Pascal has procedural and functional parameters:

    function Sort (function Compare (Key1, Key2: String): Integer);
    begin
      ...
    end;

Both ways have pros and cons, e.g. in BP you can save, compare, trade, etc. procedural values, or build arrays of them, while the SP way does not require a type declaration and prevents problems with uninitialized or invalid pointers (which in BP will usually crash the program).

GPC supports both ways. An important feature of Standard Pascal (but not BP) that GPC also supports is the possibility to pass local routines as procedural or functional parameters, even if the called routine is declared far remote. The called routine can then call the passed local routine and it will have access to the original caller's local variables.

    program LocalProceduralParameterDemo;

    procedure CallProcedure (procedure Proc);
    begin
      Proc
    end;

    procedure MainProcedure;
    var LocalVariable: Integer;

      procedure LocalProcedure;
      begin
        WriteLn (LocalVariable)
      end;

    begin
      LocalVariable := 42;
      CallProcedure (LocalProcedure)
    end;

    begin
      MainProcedure
    end.

See also: section 8.1.6.1 The Procedure, section 8.1.6.2 The Function, section 8.1.6.4 Subroutine Parameter List Declaration, section 8.1.7.11 Procedure Call.

[ < ] | [ > ] |  | [ << ] | [ Up ] | [ >> ] |  |  |  |  | [Top] | [Contents] | [Index] | [ ? ]

This document was generated by Frank Heckenbach on May, 10 2002 using texi2html