Procedures and functions

Language reference ››
Parent Previous Next

See also:        Parameter passing convention, Open array parameters, Forward procedures and functions,
External procedures and functions.


Procedures and functions are implemented very closely to the standard Pascal language:


procedure-declaration = procedure-header-declaration
{ const-declaration | type-declaration | var-declaration }
BEGIN
 { statement | block }
END ";" .
procedure-header-declaration = PROCEDURE identifier [ parameter-list ] ";" [ procedure-attributes ] [ location ] .
procedure-attributes = { interrupt-attribute | DEPRECATED [ message ] } .
interrupt-attribute = [ ( LOW | HIGH ) ";" ] [ FAST ";" ] .

function-declaration = function-header-declaration
{ const-declaration | type-declaration | var-declaration }
BEGIN
 { statement | block }
END ";" .
function-header-declaration = FUNCTION identifier [ parameter-list ] ":" simple-type ";" [ function-attributes ] [ location ] .
function-attributes = [ DEPRECATED [ message ] ] .

parameter-list = "(" parameter { "," parameter } ")" .
parameter = [ passing-convention ] identifier ":" parameter-type [ "=" parameter-default ] .
location = FORWARD | ( EXTERNAL file-name ) ";" .


Constants, types and variables declared in a procedure or function declaration are local and cannot be accessed outside.

The function result type is limited to simple types among BYTE, CHAR, SHORTINT, INTEGER, LONGINT, LONGWORD, BOOLEAN and STRING.

For PIC16+ the function result can be also of type SINGLE or REAL; it may be referenced as a pseudo variable that is defined with the name of the function (standard Pascal syntax), or as the RESULT pseudo variable (Delphi syntax) and is internally declared as:

<function-name>.RESULT.

RESULT may not be used elsewhere as a symbol name in PMP, even outside of a function (reserved word).


Since PMP is not intended for recursion (even if it is accepted but warned, use it with care), if the function result name is found in an expression, the current function value is always loaded instead of generating a recursive call.

Procedure and function arguments and local variables consume static RAM space (no stack); they are internally declared as:

<module-name>.<procedure-or-function-name>.<argument-name>.

Procedure and function local constants do not consume space (except for strings that may generate code); they are internally declared as <module-name>.<procedure-or-function-name>.<constant-name>.


Since the local variables and arguments are implemented in static RAM space, not on a stack, recursion should be used with care.



NEW! (V2.1): Parameters of simple type and of type string may have a default value. As this is now standard in Delphi and other Pascal-like languages, this feature is permanent and not an extended syntax. If a default value is specified, all further parameters must have also a default value.



PROCEDURE Setup(CONST Mode: BYTE = $55; CONST Comment: STRING = 'default');
 BEGIN
   ...
 END;
...
  Setup;
 Setup($AA);