Node:Procedural Types, Next:Object Types, Previous:Pointer Types, Up:Type Definition Possibilities

##### 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, real types, pointer types, the intrinsic `String` type. 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: The Procedure, The Function, Subroutine Parameter List Declaration, Procedure Call.