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.
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
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
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
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
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.
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).
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:
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.
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:
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.
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.
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.
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.
You can execute all the PARA-1 to PARA-N once if you use the above statement.
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.
Here, N is the numeric item literal that lets you create a hardcoded or storage constant.
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.
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.
There is also an EXIT statement that is used in COBOL74. In COBOL85, there are other statements that indicate paragraph execution end.
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.
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. 01 VAR1 PIC X(10) VALUE ‘MUTHU ‘ 01 VAR2 PIC X(10) VALUE ‘SARA ‘ 01 VAR2 PIC X(20). [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.
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.
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:
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.