TypeCobolFunctionsSyntax - TypeCobolTeam/TypeCobol GitHub Wiki

Detailled rules of procedures and functions syntax

Table of Contents

This page describes many rules regarding function or procedure declaration/call. All these rules begin with TCRFUN_.

Grammar reference

Procedure invocation syntax

CALL <procedure-name>
    <INPUT clause>?
    <INOUT clause>?
    <OUTPUT clause>?
END-CALL?

Function invocation syntax

<function-name> ( <arguments>? )

INPUT clause syntax

INPUT < <SENDING MODE clause>? <parameter-name> >+

SHARING MODE clause syntax

BY? <CONTENT | REFERENCE | VALUE>

INOUT clause syntax

INOUT <parameter-name>+

OUTPUT clause syntax

OUTPUT <parameter-name>+

Caller / How to call a function or procedure

Procedure

CALL procedure-name INPUT      inputParam1 ... inputParamN
                    IN-OUT     inoutParam1 ... inoutParamN
                    OUTPUT     outParam1   ... outParamN
end-call
if procedure-name::error-code = '0000'
   ... return OK
else 
   ... return KO, handle error
end-if

Functions

FUNCTION function-name (param1, param2, param3, ... paramN)

This syntax is the same as the syntax for COBOL intrinsic functions and can be used in the same contexts. In particular:

  • TCRFUN_TCFUNC_WHERE_COBOL85FUNC_ a custom function can be called as part of any statement where an COBOL 85 intrinsic function call would be acceptable. TODO: see if we can allow intrinsic functions in more contexts than COBOL 85.
  • TCRFUN_OPTIONAL_FUNCTION the FUNCTION keyword is optional However, if there is any ambiguity about function-name, the function call must be qualified as described in Namespaces & imports.

Alternate call for function

TCRFUN_FUNC_ALLOW_CALL Functions can also be called like a procedure. This syntax make it easier to test the error-code.

CALL function-name INPUT      inputParam1, ... inputParamN
                   RETURNING  returningParameter 
end-call
if function-name::error-code = '0000'
   ... return OK
else 
   ... return KO, handle error
end-if

Common rules for functions and procedures calls

  • Rule that need to be validated TCRFUN_FUNC_COMMA_SEPARATOR You need to separate parameters with a comma (,).

  • TCRFUN_CALL_PARAMETER_ORDER During a procedure invocation, parameters are always used in the same order they are declared in the corresponding procedure:

    1. First, all INPUT parameters (if any)
    2. Second, all INOUT parameters (if any)
    3. Third, all OUTPUT parameters (if any)
  • TCRFUN_INPUT_BY INPUT parameters can be passed by content, by reference or by value. This distinction is made for a given parameter using the SHARING MODE clause. If the SHARING MODE clause is omitted for a given parameter, the SHARING MODE of the parameter declared immediately before on the current INPUT clause is used.

    If there is no previous parameter immediately before on the current INPUT clause, then BY REFERENCE is assumed.

    For example, in the following declaration: ** i1, i4, i5, o1, o2 are passed BY REFERENCE ** i2, i3 are passed BY CONTENT

CALL myprocedure INPUT i1 BY CONTENT i2 i3 BY REFERENCE i4 i5 
                 OUTPUT o1 o2 
END-CALL.
  • TCRFUN_CALL_INOUT_AND_OUTPUT_BY_REFERENCE INOUT,OUTPUT parameters are passed by reference. The SHARING MODE clause cannot be specified.

    In other words, BY REFERENCE is always implied for INOUT,OUTPUT parameters.

Parameters matching

The caller must provide parameters that match a function or procedure declaration signature. Following rules applies to determiner if a match is successful:

  • TCRFUN_MATCH_PARAMETERS_NUMBER The number of parameters provided on each procedure call must match the number of parameters of at least one declared procedure of the same name.
  • TCRFUN_MATCH_PARAMETERS_TYPE The type of all INPUT,INOUT,OUTPUT parameters provided on a given procedure call must match the type of all parameters of at least one declared procedure of the same name.
  • Rules that need to be implemented:
  • TCRFUN_MATCH_PARAMETERS_SIZE
  • TCRFUN_MATCH_PARAMETERS_COMPRESSION

How to declare a function or procedure

Procedure declaration

DECLARE PROCEDURE procedure-name PRIVATE|PUBLIC
  INPUT       <parameter descriptions>
  IN-OUT      <parameter descriptions>
  OUTPUT      <parameter descriptions>
  ERROR-CODE  <error-code values description>.

DATA DIVISION.
    WORKING-STORAGE SECTION.
        <data declaration(s)>
PROCEDURE DIVISION.
        <COBOL and/or TypeCobol statement(s)>
END-DECLARE.

Function declaration

DECLARE FUNCTION function-name PRIVATE|PUBLIC
  INPUT       <parameter descriptions>
  RETURNING   <parameter-name>
  ERROR-CODE  <error-code values description>.

DATA DIVISION.
    WORKING-STORAGE SECTION.
        <data declaration(s)>
PROCEDURE DIVISION.
        <COBOL and/or TypeCobol statement(s)>
END-DECLARE.

Common rules for procedure and function declaration

The declaration of a procedure or a function in TypeCobol follows the same rules as the declaration of a nested program in COBOL 85, with the following differences:

  • TCRFUN_DECLARE_FUNCTION_OR_PROCEDURE
    A function is declared using the DECLARE FUNCTION statement. A procedure is declared using the DECLARE PROCEDURE statement. The FUNCTION or PROCEDURE keywords are not mandatory. If both of these keywords are omitted, the following rules apply:

    • The nature of what is declared is decided according to the rules TCRFUN_NO_INOUT_OR_OUTPUT_FOR_FUNCTIONS and TCRFUN_NO_RETURNING_FOR_PROCEDURES.
    • If the parameter list declaration uses both the RETURNING keyword and any of the IN-OUT or OUTPUT keywords, the DECLARE statement will be in error, as what is declared cannot be either a function or a procedure.
    • If there are only input parameters (or no parameter at all) and no in-out, output or returning parameter, the FUNCTION keyword is assumed.
  • TCRFUN_FIXED_FORMAT In fixed column format, all keywords related to function or procedure, can start at any column between columns 8 and 72 included.

  • TCRFUN_DEFAULT_ACCESS_MODIFIER Procedure or Function declaration will not need any of the PRIVATE or PUBLIC access modifier. The default Access modifier is PRIVATE if none is declared.

    • The PUBLIC phrases gives a public visibility to the procedure or function.
    • The PRIVATE phrases gives a private visibility to the procedure or function. Only the enclosing program or others procedures and functions inside the same program can call it.
  • TCRFUN_DOTS A . is mandatory only after the parameters and return-code declaration.

  • TCRFUN_NO_DOT_AFTER_VISIBILITY There is no dot after function or procedure visibility.

  • TCRFUN_NO_PERFORM_OF_ENCLOSING_PROGRAM
    A function or procedure cannot use any PERFORM statement that targets a paragraph or section of the enclosing program.

Parameters description (input, in-out, output and returning):

  • TCRFUN_PARAMETER_DECLARATION_ORDER Parameters are always declared in this order: input, in-out, output, returning.

  • TCRFUN_PARAMETER_DESCRIPTION A parameter description must contains the following:

    • a parameter name (ex: dateToConvert)
    • either one PICTURE clause or one TYPE clause
    • Restrictions:
      • No level descriptor, except for level 88. Level 01 is implied in all cases when 88 is not present.
      • No REDEFINES
      • No RENAMES
      • Neither FILLER nor unnamed variables
      • Neither GLOBAL nor EXTERNAL
      • Neither BY REFERENCE, nor BY VALUE, nor BY CONTENT can be specified: BY REFERENCE is implied in all cases.
  • TCRFUN_0_TO_N_PARAMETERS Lists of input, in-out and output parameters can contain 0 to N parameters.

    • The list of input parameters can be empty. In this case, the function/procedure takes no input parameters and the INPUT keyword must not be used.
    • The list of in-out parameters can be empty. In this case, the procedure takes no in-out parameters and IN-OUT keyword must not be used.
    • The list of output parameters can be empty. In this case, the procedure takes no output parameters and OUTPUT keyword must not be used.
  • TCRFUN_NO_INOUT_OR_OUTPUT_FOR_FUNCTIONS A function can only declare input parameters (using the INPUT clause) and one returning parameter (using the RETURNING clause). A function cannot declare in-out or output parameters.

  • TCRFUN_NO_RETURNING_FOR_PROCEDURES A procedure cannot have a returning parameter.

  • TCRFUN_0_TO_1_RETURNING_PARAMETER A function has exactly one returning parameter. The declaration of returning parameter is mandatory, but it can be omitted (RETURNING OMITTED).

  • TCRFUN_NO_COPY_IN_PARAMETERS A parameter cannot be described by a COPY.

Level 88 parameter description

TCRFUN_LEVEL_88_PARAMETERS Level 88 parameters are declared like a standard COBOL 85 parameters: **TODO: see if we can provide enum grammar, instead of levels 88 **

DECLARE FUNCTION function-name PRIVATE
  input     typeOfTransportpic X
              88 typeOfTransport-Car  value 'C'
              88 typeOfTransport-Bike value 'B'.

Error-code values

  • TCRFUN_ERROR_CODE_DECLARATION_ORDER error-code values are always declared after input, in-out, output and returning parameters.
  • TCRFUN_ERROR_CODE_OPTIONAL Error-code declaration is optional for procedure and function.

See page Error for more details about error management.

Other Rules:

TCRFUN_DECLARATION_AS_NESTED_PROGRAM A procedure or a function is declared like a nested program. The declaration is inside the procedure division and outside of any section or paragraph.

Compared to a nested program declaration, a procedure or function declaration has the following exceptions:

  • TCRFUN_DECLARATION_NO_IDENTIFICATION_DIVISION There is no IDENTIFICATION DIVISION

  • TCRFUN_DECLARATION_NO_ENVIRONMENT_DIVISION There is no ENVIRONMENT DIVISION

  • Inside the DATA DIVISION:

    • TCRFUN_DECLARATION_NO_FILE_SECTION There is no FILE SECTION
    • Restricted data declaration inside WORKING-STORAGE, LOCAL-STORAGE and LINKAGE
      • TCRFUN_DECLARATION_NO_GLOBAL GLOBAL are not allowed
      • TCRFUN_DECLARATION_NO_EXTERNAL EXTERNAL are not allowed
      • TCRFUN_DECLARATION_NO_DUPLICATE_NAME No LINKAGE SECTION item can have the same name as a parameter declared as INPUT, OUTPUT, IN-OUT or RETURNING.
  • PROCEDURE DIVISION is equivalent to COBOL 85 program, with the following exceptions:

    • TCRFUN_DECLARATION_NO_USING The USING phrase is not allowed.
  • TCRFUN_MANDATORY_END_DECLARE The END-DECLARE. phrase is mandatory.

  • TCRFUN_LIBRARY_PROCEDURE_NO_USING A library PROCEDURE DIVISION must not declare any USING clause.

  • TCRFUN_ONLY_PARAGRAPH_AND_PUBLIC_FUNC_IN_LIBRARY f you declare a PUBLIC procedure or function, then you can only write PARAGRAPH declaration inside the same PROCEDURE DIVISION. You can only declare PUBLIC procedure or functions inside this PROCEDURE DIVISION. A special paragraph named INIT-LIBRARY will be called when the library is called.

Where to put this declaration?

It depends on the procedure visibility: private or public

Private procedure

Private procedure declaration must be put outside paragraph and section declaration.

IDENTIFICATION DIVISION.
PROGRAM-ID. TypeCobol.
data division.
working-storage section.
01 a pic 9(04).
01 b pic 9(04).
01 c pic 9(04).
PROCEDURE DIVISION.
   move 1 to a
   move 2 to b
   perform MyParagraph
.
MyParagraph.
   CALL MyProcedure input(a, b)
                    output(c)
.
declare procedure MyProcedure private
  input  param1 pic 9(04)
         param2 pic 9(04)
  output outParam1 pic 9(04).
procedure division.
   compute outParam1 = param1 + param2 
   .
end-declare.
.
 END PROGRAM.

Public procedure

If you want to declare at least one public procedure, then you can't write section or paragraph inside procedure division. All code inside procedure division must be put inside functions or procedures declaration. The only way to call your program is through a public function or procedure.

Technically all functions and procedure will be generated into a sub-programs and your procedure division will contains code to call theses sub-programs.

IDENTIFICATION DIVISION.
PROGRAM-ID. TypeCobol.
PROCEDURE DIVISION.
declare procedure MyProcedure private
 input  param1    pic 9(04)
 output outParam1 pic 9(04).
procedure division.
   ...
   .
end-declare.
declare function MyPublic public
 input     param1 9(04)
 returning result type bool.
 procedure division.
   ...
   .
end-declare.
.
END PROGRAM.
⚠️ **GitHub.com Fallback** ⚠️