[Cobol] How do I create and call a function in Cobol?

To create and call a function in COBOL, you can follow these steps:

  1. Define the function name and parameters (if any) in the DATA DIVISION of the program:
1DATA DIVISION.
2   WORKING-STORAGE SECTION.
3   01 FUNCTION-NAME PIC X(10) VALUE 'MY-FUNCTION'.
4   01 PARAMETER-1 PIC X(10).
5   ...
  1. Implement the function logic in the PROCEDURE DIVISION using the FUNCTION verb:
1PROCEDURE DIVISION.
2   ...
3   MOVE 'Hello' TO PARAMETER-1.
4   CALL FUNCTION FUNCTION-NAME
5       USING PARAMETER-1
6       RETURNING PARAMETER-1.
7   ...
  1. Define the function using the FUNCTION-ID paragraph in the PROCEDURE DIVISION:
1FUNCTION-ID. MY-FUNCTION.
2   ENTRY 'MY-FUNCTION' USING PARAMETER-1
3                         RETURNING PARAMETER-1.
  1. Write the logic of the function within the MY-FUNCTION paragraph:
1MY-FUNCTION.
2   ...
3   PERFORM SOME-LOGIC
4   ...
5   MOVE 'World' TO PARAMETER-1.
6   ...
  1. Finally, you can call the function within your program by simply using the CALL statement:
1CALL FUNCTION-NAME USING PARAMETER-1
2                      RETURNING PARAMETER-1.

In this example, the function MY-FUNCTION is called using the CALL statement with PARAMETER-1 passed as a parameter. The function will modify PARAMETER-1 and return it to the calling program.