TOP-10 COBOL Functions You Should Learn - ByteScout
Announcement
Our ByteScout SDK products are sunsetting as we focus on expanding new solutions.
Learn More Open modal
Close modal
Announcement Important Update
ByteScout SDK Sunsetting Notice
Our ByteScout SDK products are sunsetting as we focus on our new & improved solutions. Thank you for being part of our journey, and we look forward to supporting you in this next chapter!
  • Home
  • /
  • Blog
  • /
  • TOP-10 COBOL Functions You Should Learn

TOP-10 COBOL Functions You Should Learn

Are you looking to learn about COBOL functions? If you do, then you have come to the right place. In this article, we will explore the top 10 COBOL functions. These functions will help you take full advantage of the COBOL programming. So, without any delay, let’s get started.

Top 10 COBOL Functions

Most Important COBOL Functions

1. ADD Verb

The ADD function lets you sum two (or more) numeric operands.

Format 1: Add OPERAND-1 to OPERAND-2

As you can see, you need to use the TO keyword, to sum up, two or more operands together.

Examples: ADD COST TO PAID

2. SUBTRACT Verb

Just like the ADD Verb, the SUBTRACT statement is used to subtract one operand from another.

Format 1: SUBTRACT OPERAND-1 From OPERAND-2

Here, we need to use the FROM keyword to do the operation.

Examples: SUBTRACT DISCOUNT FROM PRICE

3. MULTIPLY Verb

If you want to multiply two operands in COBOL, then you need to use the MULTIPLY function. Here, the two operand values are multiplied and stored.

Format 1: MULTIPLY OPERAND-1 BY OPERAND-2

As you can see here, we have multiplied OPERAND-2 and OPERAND-1 together with their result stored.

Examples: MULTIPLY PRICE BY AMOUNT

4. DIVIDE Verb

The DIVIDE statement enables you to divide one numeric data item by another one. The result is then stored in a reminder and quotient.

Format 1: DIVIDE OPERAND-1 INTO OPERAND-2

As you can see, we used INTO keyword for dividing OPERAND-1 by OPERAND-2.

Example: DIVIDE TOTAL_PRICE INTO QUANTITIES

5. COMPUTE Verb

COMPUTE statement is used to store the value of an arithmetic operation that is done on two operands. This means that the right-hand side value is assigned to the left-and-side data item.

Rule: Left to right.

  1. Parentheses (‘(‘’)’)
  2. Exponentiation (**)
  3. Multiplication and Division (* and /)
  4. Addition and Subtraction (+ and -)

Also, you should be careful when it comes to using ROUNDED verbs with COMPUTE. This can throw off the compiler as it will try to round the arithmetic operations. So, the end result will not be precise.

It is also suggested that you use the explicit scope terminators with all arithmetic operators such as END-SUBTRACT, END-ADD, END-DIVIDE, END-MULTIPLY, END-COMPUTE).

6. ACCEPT Verb

ACCEPT verb is used to transfer the data from system information or an input device such as DATE, TIME, DAY reserved data.

Format: ACCEPT VAR-WS(FROM TIME/DATE/OTHER SYSTEM VARS/DAY)

The data will be read from the terminal even if the FROM Clause is not coded. The batch program at the time of execution is shifted to ABEND if there is no FROM clause or no in-stream data from JCL. These are retrieved from the ACCEPT clause.

Examples:

  • DATE option → returns YYMMDD, six-digit current date
  • DAY option → returns YYDDD, 5 digits current date
  • TIME option → returns HHMMSSTT, RUN TIME, 8 digit
  • DAY-OF-Week option → returns Monday-Sunday respectively through a single digital 1-7

7. IF/THEN/ELSE/END-IF

As a COBOL developer, you will find the conditional statement useful for coding conditions. The most frequently used decision-making statement that you will use is “IF”. You can use the IF without the need to use the ELSE statement. You can also use THEN statements which are optional.

You can also use ANDs and ORs statements in one sentence. However, the evaluation is done from left to right in the favor of ANDs which is then followed by ORs. Also, you can override this behavior with the help of parenthesis.

So, what are the permitted relation conditions and their sequence of execution? It is:

=, <, >, <=, >=, <>

There is another useful statement: CONTINUE. However, there is no operating statement. It simply passes the control(flow) to the next statement in the program. Also, to make things clear, a Sentence is defined as statements that are ended with a full stop.

As a developer, you should also use END-IF which will help you determine and fix the scope of the IF statements.

IF condition1 AND condition2 THEN
   Statement-Block-1
ELSE
   IF condition3 THEN
      CONTINUE
   ELSE
      IF condition4 THEN
         Statement-Block-2
      ELSE
         NEXT SENTENCE
      END-IF
   END-IF
END-IF

In the above code, you can see that conditions 1, 2, and 4 needs to be TRUE and Condition 3 to be FALSE for the Statement-Block-2 to be executed.

Lastly, we have the Implied operand. This operation is used in compound conditions where it is not possible to specify operands for each condition. So, TOTAL=8 is an implied operation whereas IF TOTAL = 7 or 8.

8. EVALUATE

The next COBOL function that we are going to discuss is the EVALUATE verb. It is made available in COBOL85 and is used to implement a case structure.

So, if you have multiple IF statements, you can use EVALUATE instead of it. This improves code readability and also helps you improve code quality. So, if the condition of the EVALUATE  is not met, then the program control is then given to the statement that proceeds END-EVALUATE.

EVALUATE SYNTAX:


EVALUATE subject-1 (ALSO subject2..)
WHEN object-1 (ALSO object2..)
WHEN object-3 (ALSO object4..)
WHEN OTHER imperative statements
END-EVALUATE

Things to notice from the above code:

  • The EVALUATE close subject number can be similar to the WHEN clause object numbers.
  • The subjects can be expressions, variables, or keywords such as TRUE/FALSE. The respective object can be values or any condition including TRUE/FALSE.
  • The WHEN another path is executed if no WHEN condition is satisfied.

Sample:

EVALUATE SQLCODE ALSO TRUE
WHEN 100 ALSO A=B
WHEN  -305 ALSO (A/C=4)
   DISPLAY  ‘ALLOWED SQLCODE..PROCEEDING..’
WHEN OTHER imperative statements
END-EVALUATE

The above example shows how the WHEN statements perform when they are TRUE.

9. PERFORM STATEMENTS

If you want to execute a set of statements over and over again through the program structure, then you can opt to use the PERFORM function.

To do so, you need to invoke PERFORM by writing one paragraph that contains all the statements. Also, the control of the program goes back to the next statement of PERFORM once the paragraph is executed.

Let’s see different ways you can use the PERFORM statement.

1.SIMPLE PERFORM.

PERFORM PARA-1.
DISPLAY ‘PARA-1 executed’
STOP RUN.
PARA-1.
   Statement1
   Statement2.

Here, you can see that all the PARA-1 instructions are executed. After that, the control of the program is transferred to the next statement.

2.INLINE PERFORM.

PERFORM
   ADD A TO B
   MULTIPLE B BY C
   DISPLAY ‘VALUE OF A+B*C ‘ C
END-PERFORM

Here, we do an INLINE PERFORM where the set of statements is used once and hence can be grouped together within the PERFORM END-PERFORM structure. If you do so, you will be doing an INLINE PERFORM. This is similar to the DO.END structure that you do in other programming languages.

3. PERFORM PARA-1 THRU PARA-N

You can execute all the PARA-1 to PARA-N once if you use the above statement.

4. PERFORM PARA-1 THRU PARA-N UNTIL condition(s).

To make PARA-1 to PARA-N perform multiple times(say N), then you have to go this way. Also, to do so, you need to change the UNTIL condition(s) identifiers when they perform. If you don’t, there will be an indefinite paragraph performance.

5. PERFORM PARA-1 THRU PARA-N N TIMES

Here, N is the numeric item literal that lets you create a hardcoded or storage constant.

6. PERFORM PARA-1 THRU PARA-N VARYING identifier1

FROM identifier 2 BY identifier 3 UNTIL conditions(s)

Here, we test the condition(s), along with initializing identifier 1 with identifier2. The statements in the PARA-1 thru PARA-N are executed if the condition is false. The identifier1 is also incremented by identifer3, then the conditions are checked again. This is done until the conditions are satisfied.

7. PERFORM PARA-1 WITH TEST BEFORE/AFTER UNTIL condition(s).

Here, the PARA-1 is executed after the condition is checked and found false with TEST BEFORE. It is similar to DO-WHILE in other programming languages.

In the case of TEST AFTER, the condition is checked and then PARA-1 is executed once. This is similar to DO-UNTIL in other programming languages.

8. EXIT STATEMENT

There is also an EXIT statement that is used in COBOL74. In COBOL85, there are other statements that indicate paragraph execution end.

9. GO TO Usage

PERFORM 100-STEP1 THRU STEP-4
   ..
100-STEP-1.
   ADD A TO B GIVING C.
   IF D = ZERO DISPLAY ‘MULTIPLICATION NOT DONE’
               GO TO 300-STEP3
   END-IF.
200-STEP-2.
   MULTIPLY C BY D.
300-STEP-3.
   DISPLAY ‘VALUE OF C:’ C

The GOTO statement is not preferable when it comes to structured top-down programming. The GO TO statements are used to transfer the program’s control from one statement to another. When used with paragraphs, it transfers the program control from one paragraph to another. However, you should rarely use it as it can badly affect the program’s readability.

But, you can use GOTO in certain cases. For example, you can use the THRU statement from the PERFORM statement as shown in the above example. Also, GO TO statements are fine if they are in the scope of the named paragraphs.

10. STRING

If you want to concatenate one string to another then you need to use the STRING command.

Syntax:

    STRING identifier-1 / literal-1, identifier-2/ literal-2
    DELIMITED BY (identifier-3/literal-3/SIZE)
    INTO identifier-4
    END-STRING.

&nbsp;
01 VAR1 PIC X(10) VALUE ‘MUTHU    ‘
01 VAR2 PIC X(10) VALUE ‘SARA      ‘
01 VAR2 PIC X(20).

&nbsp;
[vb]
To get display ‘MUTHU, SARA’
  STRING VAR1 DELIMITED BY ‘ ‘
    ‘,’ DELIMITED BY SIZE
      VAR2 DELIMITED BY ‘ ‘
   INTO VAR3
END-STRING.

Here, you can see that the receiving field needs to have no editing symbols, but an elementary data item. Also, it should be JUST RIGHT clause.

Also, you can replace it with the MOVE where the string’s specific characters can be replaced.

01 AGE-OUT PIC X(12) VALUE ’12 YEARS OLD’.
STRING ‘18’ DELIMITED BY SIZE INTO AGE-OUT.  => 18 YEARS OLD.

11. UNSTRING

You can use the UNSTRING command to separate a string into multiple ones.

 

Syntax:

   UNSTRING  identifier-1
   [DELIMITED BY (ALL/) identifier2/literal1 [,OR (ALL/) (identifier-3/literal-2),..]]
   INTO identifier-4 [,DELIMITER IN identifier-5, COUNT IN identifier-6]
   [,identifier-7 [,DELIMITER IN identifier-8, COUNT IN identifier-9]

 

01 WS-DATA PIC X(12) VALUE ‘10/200/300/1’.
UNSTRING WS-DATA DELIMITED BY ‘/’
  INTO WS-FLD1 DELIMITER IN WS-D1 COUNT IN WS-C1
    WS-FLD2 DELIMITER IN WS-D2 COUNT IN WS-C2
    WS-FLD3 DELIMITER IN WS-D3 COUNT IN WS-C3
END-UNSTRING.

 

Result:

WS-FLD1 = 10 WS-FLD2 =200 WS-FLD3=300
WS-C1 = 2 WS-C2=3 WS-C3=3 WS-D1 = ‘/’ WS-D2=’/’ WS-D3 ‘/’

 

STRING and UNSTRING can be coded ON OVERFLOW.

Also, if a STRING truncation takes place then the imperative statements are executed that are after the ON OVERFLOW.

12. COPY Statement

The last statement that we are going to discuss is the COPY statement. It is used to bring pre-written COBOL entries into the program.

To use it effectively, you need to keep the following in mind:

  • You can use COPY to bring common routines such as data validation routine or error routine
  • There are master files that are used in multiple programs. That means that you can use their layout from one copybook to another. By doing so, COBOL follows standardization, and hence the program looks and feels the same with similar data names and layout.

By doing so, the debugging time is reduced and there is no need to change the layout every time you code.

Syntax:

COPY copybook-name [(OF/IN) library name]
[REPLACING string-to-be-replaced BY replacing-string]

Copybooks are included in the program during compilation time. Otherwise, they are kept in the PDS library. You can change the copybook library by using the OF of the CPY statement or IN statement. By default, it is set to SYSLIB.

   

About the Author

ByteScout Team ByteScout Team of Writers ByteScout has a team of professional writers proficient in different technical topics. We select the best writers to cover interesting and trending topics for our readers. We love developers and we hope our articles help you learn about programming and programmers.  
prev
next