TypeCobolFunctionsSyntax - TypeCobolTeam/TypeCobol GitHub Wiki
This page describes many rules regarding function or procedure declaration/call. All these rules begin with TCRFUN_.
- Grammar reference
- Caller / How to call a function or procedure
- How to declare a function or procedure
- Code generation
CALL <procedure-name>
<INPUT clause>?
<INOUT clause>?
<OUTPUT clause>?
END-CALL?
<function-name> ( <arguments>? )
INPUT < <SENDING MODE clause>? <parameter-name> >+
BY? <CONTENT | REFERENCE | VALUE>
INOUT <parameter-name>+
OUTPUT <parameter-name>+
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
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.
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
-
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:
- First, all
INPUT
parameters (if any) - Second, all
INOUT
parameters (if any) - Third, all
OUTPUT
parameters (if any)
- First, all
-
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 currentINPUT
clause is used.If there is no previous parameter immediately before on the current
INPUT
clause, thenBY REFERENCE
is assumed.For example, in the following declaration: **
i1
,i4
,i5
,o1
,o2
are passedBY REFERENCE
**i2
,i3
are passedBY 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 forINOUT
,OUTPUT
parameters.
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
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.
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.
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 theDECLARE FUNCTION
statement. A procedure is declared using theDECLARE PROCEDURE
statement. TheFUNCTION
orPROCEDURE
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 theIN-OUT
orOUTPUT
keywords, theDECLARE
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.
- The
-
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 anyPERFORM
statement that targets a paragraph or section of the enclosing program.
-
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 oneTYPE
clause - Restrictions:
- No level descriptor, except for level
88
. Level01
is implied in all cases when88
is not present. - No
REDEFINES
- No
RENAMES
- Neither
FILLER
nor unnamed variables - Neither
GLOBAL
norEXTERNAL
- Neither
BY REFERENCE
, norBY VALUE
, norBY CONTENT
can be specified:BY REFERENCE
is implied in all cases.
- No level descriptor, except for level
- a parameter name (ex:
-
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.
- The list of input parameters can be empty. In this case, the function/procedure takes no input parameters and the
-
TCRFUN_NO_INOUT_OR_OUTPUT_FOR_FUNCTIONS A function can only declare input parameters (using the
INPUT
clause) and one returning parameter (using theRETURNING
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.
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'.
- 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.
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
andLINKAGE
-
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 asINPUT
,OUTPUT
,IN-OUT
orRETURNING
.
-
TCRFUN_DECLARATION_NO_GLOBAL
-
TCRFUN_DECLARATION_NO_FILE_SECTION There is no
-
PROCEDURE DIVISION
is equivalent to COBOL 85 program, with the following exceptions:-
TCRFUN_DECLARATION_NO_USING The
USING
phrase is not allowed.
-
TCRFUN_DECLARATION_NO_USING The
-
TCRFUN_MANDATORY_END_DECLARE The
END-DECLARE.
phrase is mandatory. -
TCRFUN_LIBRARY_PROCEDURE_NO_USING A library
PROCEDURE DIVISION
must not declare anyUSING
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.
It depends on the procedure visibility: private or public
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.
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.