COBOL FUNCTIONS:

COBOL functions offer a new way to manipulate data that was not available in older versions. We will look at a series of COBOL functions in this handout. Functions are available for: mathematics, character manipulation, date/time and statistics and financial work. They can be used throughout the Procedure Division. Functions are elementary data items that will return a numeric (whole or decimal) or an alphanumeric. A function cannot be used as a receiving operand. When using a function, you must always use the word FUNCTION followed by the specific function you are using following by any required or optional arguments.

We are going to look at some common function use in this handout. Please check HELP for more FUNCTIONS.

UPPER-CASE & LOWER-CASE FUNCTIONS:

If you want to compare two fields and you are not sure of what case the fields contain, you can look at the upper case version of both fields or the lower case version of both fields so that you are comparing like configurations.

IF FUNCTION UPPER-CASE(FLD1) = FUNCTION UPPER-CASE(FLD2)
>     PERFORM B-300-MATCH-ROUT.

IF FUNCTION LOWER-CASE(FLD1) = FUNCTION LOWER-CASE(FLD2)
    PERFORM B-300-MATCH-ROUT.

If you are concerned that a user might enter data incorrectly at the screen, you can covert it using this function.

MOVE FUNCTION UPPER-CASE(STATE-IN) TO STATE-FOR-FILE.

LENGTH FUNCTION:

The length function gets the length of non-numeric fields so that you can find out the length of the field.

COMPUTE LEN1 = FUNCTION LENGTH(FLD-IN).

 

REVERSE FUNCTION:

If you need to reverse the data in a field you can use the REVERSE function. This means if the data was entered as XYZ, the new field will now be ZYX.

MOVE FUNCTION REVERSE(FLDA) TO FLDB.

SUM, MAX, MIN FUNCTION:

Will get the sum, max or min of the arguments in parenthesis.

COMPUTE TOTAL-WS = FUNCTION SUM(FLDA FLDB FLDC FLDD).

COMPUTE MAX-WS = FUNCTION MAX(FLDA FLDB FLDC FLDD).

COMPUTE MIN-WS = FUNCTION MIN(FLDA FLDB FLDC FLDD).

CONVERT TO NUMERIC FUNCTION:

This will convert an edited field to a numeric for use in a calculation. ANS-WS will contain 2367886 with an assumed decimal point between the two 8s. Its PIC would be S9(5)V99. Note, this should be done as a mathematical conversion prior to a MOVE statement, it should not be done in the MOVE etc.

COMPUTE ANS-WS = FUNCTION NUMVAL-C("$23,678.86 CR").

If you just want to convert an alphanumeric field to numeric, you would use NUMVAL.

COMPUTE ANS-WS = FUNCTION NUMVAL(FLDX).

If you had a table of numeric data and you wanted to sum that data or find the maximum value in that data, you can do it with the function and instead of using a subscript to move through the table, you can simply code ALL.

COMPUTE MAX-AMT-WS = FUNCTION MAX(NUM-TABLE(ALL)).

COMPUTE TOT-AMT-WS = FUNCTION SUM(NUM-TABLE(ALL)).

INTEGER FUNCTIONS:

This will take the integer and assign it to INT-ANS-WS. Obviously you could have a field name in the parenthesis. If the decimal is greater than 5, it will round. If you want to truncate, use INTEGER-PART.

COMPUTE INT-ANS-WS = FUNCTION INTEGER(2123.456). returns 2123

COMPUTE INT-ANS-WS = FUNCTION INTEGER(45.7). returns 46

COMPUTE INT-ANS-WS = FUNCTION INTEGER-PART(123.456). returns 123

COMPUTE INT-ANS-WS = FUNCTION INTEGER-PART(63.78). returns 63

REMAINDER VALUE FUNCTION:

COMPUTE REM-WS = FUNCTION REM(42,5). returns 2 since 42/5 has a remainder of 2.

DATE, TIME FUNCTIONS:

You need to note that date functions use the Gregorian calendar which means that January 1, 1601 is day 1 of the calendar.

FUNCTION CURRENT-DATE will get the current data and return it as a field that takes 21 characters - PIC X(21). The format is yyyymmddhhmmsstt+hhmm. Note that tt is hundredths of seconds and the hhmm at the end is the relation to Greenwich Mean Time.

FUNCTION DATE-OF-INTEGER(Gregorian date in days) will return yyyymmdd while FUNCTION DAY-OF-INTEGER(Gregorian date in days) will return yyyyddd - the days here are in Julian ranging from 1 to 366.

FUNCTION INTEGER-OF-DATE(date as yyyymmdd) will return the Gregorian date.

FUNCTION DATE-TO-YYYYMMDD(date,n) will convert a date to a four digit year. The date is given in YYMMDD and the n is an integer that defines the ending year of the relevant 100 year interval in relation to the current date. The way I understand this is that since we are in 2000, making n 15 would mean the date is in the hundred year range ending in 2015. You should read up on this if you ever use it!!! FUNCTION YEAR-TO-YYYY works the same way, but just with years.