A function performs an operation which creates a reference to a data item derived from existing data item(s) automatically during execution.
A function has a name and an argument list, and can return value.
Argument types can be:
alphabetic
alphanumeric
national
numeric
integer
DBCS
UTF‑8
pointer types
keywords
Functions can be:
intrinsic (built-in in COBOL compiler)
user-defined
The intrinsic functions are fundamental operations implemented inside the COBOL compiler, not external routines. All intrinsic functions as part of the COBOL compiler’s native feature set:
predefined
built‑in
always available
optimized by the compiler
not stored in any library
not written in COBOL source code
Referencing (calling) a function is possible from inside the PROCEDURE DIVISION.
These functions are creatable in modern COBOL and defined by the developer:
has an own name
receives parameters
generates a return value
exists as an independent and re-usable unit
compiles independently from the program
callable via the FUNCTION syntax
The received (input) argument can be optionally defined as:
BY VALUE (copies the value of the sent variable)
BY REFERENCE (receives the pointer to the original variable, any changes will have effect on the original on caller side; default)
The return value can be:
elementary item
group item
numeric
alphanumeric
pointer
UTF‑8
DBCS
EXAMPLE FOR USER-DEFINED FUNCTION:
EXAMPLE FOR CALLING USER-DEFINED FUNCTION:
The UDFs must be compiled (IGYCRCTL) independently and beforehand, and must be linked with the caller program in JCL (IEWL) or USS (cob).
Advantages of using UDFs:
modular code
re-usability
clean architecture
easier tests
modern COBOL compatibility
independent compilation (main program builds faster)
can be versioned
Limits of UDFs:
only one return value
only one exit point (no STOP RUN)
can not access variables of the caller program except what was sent
In modern COBOL hierarchy modular programming has two main legs: modules (without return values) and functions (with return values). See COBOL PROGRAM MODULES for more.